java - Android 通过 HTTP 发送数据并接收正文响应

标签 java android

我看到 Android API 22 DefaultHttpClient 版本已弃用。在我的旧代码中,我必须将数据发送到 PHP 页面并获得页面 php 返回的响应。通过使用此代码:

HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("php page"); 

ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", "user"));
parameters.add(new BasicNameValuePair("password", "pw"));

HttpResponse response = client.execute(request);
String responseMessage = response.getEntity().toString();

我看到 NameValuePair 已被弃用。我的问题是我怎样才能以另一种不推荐使用的方式做同样的事情。正如我所说,我需要将数据(通过邮寄)发送到 php 页面并获取 PhP 打印的页面。 (在我的例子中是一个字符串 JSON)。非常感谢!!

最佳答案

使用 HttpURLConnection。将您的值设置为 JSONObject 而不是将其设置为 NameValuePair。

public HttpURLConnection createConnection(){

    try{            
        urlconnection=(HttpURLConnection)url.openConnection();      
    }catch(Exception e){
        Log.e("Can't create connections", e.getMessage());

    }

    return urlconnection;

}
public String doFunctionPost(JSONObject object){
    try{
        urlconnection=createConnection();
        urlconnection.setDoInput(true);
        urlconnection.setDoOutput(true);
        urlconnection.setRequestMethod(method);
        urlconnection.setUseCaches(false);
        urlconnection.setConnectTimeout(10000);
        urlconnection.setReadTimeout(10000);
        urlconnection.setRequestProperty("json", object.toString());
        urlconnection.setRequestProperty("Content-Type", "application/json");

        OutputStreamWriter out=new OutputStreamWriter(urlconnection.getOutputStream());
        out.write(object.toString());
           System.out.println(object.toString());
            out.close();
            httpResult=urlconnection.getResponseCode();
            System.out.println("Response Code"+ httpResult);
            result=readResponse();

    }catch(Exception e){
        Log.e("Response Error", e.getMessage());

    }finally{
        removeConnection();
    }
    return result;
}

public String readResponse(){
    try{
        if(httpResult==HttpURLConnection.HTTP_OK){
            BufferedReader buffer_reader=new BufferedReader(new InputStreamReader(urlconnection.getInputStream(),"utf-8"));
            String line=null;
            sb=new StringBuilder();
            while((line=buffer_reader.readLine())!=null){
                sb.append(line+"\n");
            }
            buffer_reader.close();
            System.out.println("buffer_reader"+sb.toString());
        }else{
            Log.e("Error on posting", urlconnection.getResponseMessage());
        }
    }catch(Exception e){
        Log.e("Error in Response", e.getMessage());
    }
    return sb.toString();
}

关于java - Android 通过 HTTP 发送数据并接收正文响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33441923/

相关文章:

java - 更改java中不同类的标签文本

java 小程序在浏览器最新版本中被阻止

php - Laravel 消费者和提供者应用架构

Android OpenGL ES 透明背景

java - 更改数据值后如何重新启动Handler?

java - 在spring jsp中将输入文本和隐藏字段的数据发送到 Controller 名称dosavenews

java - 如何将具有两组元素的列表排序为两个按字母顺序排列的集合列表

java - Canvas 绘制速度太慢

android - "Parcelable protocol requires a Parcelable.Creator object called CREATOR"(我有 CREATOR)——在 Kotlin 中

android - Unity3d 中的 map 集成