httpurlconnection_get_post


//utils帮助类

package com.example.httpurlconnection_get_post;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;


public class httpURlUlist {
    
    
public static String getUlits(String name,String password){
    String path="http://172.19.108.2:8080/fuwuqi/Fuwuqi?name="+name+"&preass="+password;
    
    try {
    URL     url = new URL(path);
        try {
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            //设置读取服务器时间
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
            int responseCode = connection.getResponseCode();
            
            System.out.println(connection.getResponseCode());
            if (responseCode==200) {
                System.out.println(connection.getResponseCode());
                InputStream inputStream = connection.getInputStream();
                String str="";
                BufferedReader   bf=new BufferedReader(new InputStreamReader(inputStream));
                str=bf.readLine();
                //
                return str;
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    
    return null;
    
}
public static String postUlits(String name,String password) throws IOException{
    String path="http://172.19.108.2:8080/fuwuqi/Fuwuqi";
    String paths="name="+name+"preass="+password;
    try {
        URL url=new URL(path);
        HttpURLConnection connection=(HttpURLConnection) url.openConnection();
        connection.setRequestMethod("post");
        connection.setDoOutput(true);
        connection.setReadTimeout(5000);
        connection.setConnectTimeout(5000);
        connection.getOutputStream().write(paths.getBytes());
        if (connection.getResponseCode()==200) {
            return EntityUtils.toString((HttpEntity) connection.getErrorStream());
            
        }
        
        
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    
    return null;
    
}

}



//main主方法

package com.example.httpurlconnection_get_post;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

    private EditText editText1;
    private EditText editText2;
    private String name;
    private String password;
    private String name2;
    private String password2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    editText1 = (EditText) findViewById(R.id.editText1);
    editText2 = (EditText) findViewById(R.id.editText2);
    }

public void getway(View view){
        name = editText1.getText().toString().trim();
        password = editText2.getText().toString().trim();
    
    new Thread(new Runnable() {
        
        @Override
        public void run() {
            String ulits = httpURlUlist.getUlits(name, password);
            if (ulits!=null) {
                Log.e("xxx", ulits);
            }
            else {
                Log.e("xxx", "空");
            }
            
        }
    }).start();
    
}
public void postway(View view){
    name2 = editText1.getText().toString().trim();
    password2 = editText2.getText().toString().trim();
    new Thread(new Runnable() {
    
    @Override
    public void run() {
        try {
            String ulits = httpURlUlist.postUlits(name2, password2);
            if (ulits!=null) {
                Log.e("xxx", ulits);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}).start();
}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用 Java 代码编写 HttpURLConnection 发送 GET 和 POST 请求的示例: 1. 发送 GET 请求 ```java import java.net.*; import java.io.*; public class HttpGet { public static void main(String[] args) { try { URL url = new URL("http://example.com"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } ``` 2. 发送 POST 请求 ```java import java.net.*; import java.io.*; public class HttpPost { public static void main(String[] args) { try { URL url = new URL("http://example.com"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Accept", "application/json"); conn.setDoOutput(true); String input = "{\"username\":\"test\",\"password\":\"test\"}"; OutputStream os = conn.getOutputStream(); os.write(input.getBytes()); os.flush(); if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } ``` 注意,在发送 POST 请求时需要设置 `Content-Type` 和向输出流中写入请求体。如果需要发送其他类型的请求,可以根据需要修改 `setRequestMethod` 和请求头部。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值