Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从HttpClient到HttpURLconnection_Android_Httpclient_Httpurlconnection - Fatal编程技术网

Android 从HttpClient到HttpURLconnection

Android 从HttpClient到HttpURLconnection,android,httpclient,httpurlconnection,Android,Httpclient,Httpurlconnection,我想升级发布json的代码。 第一个实现了HttpClient,它工作得很好! 我尝试使用HttpURLconnection使用新的实现,但它不起作用!我不能发送任何邮寄请求。 我错过了什么 public class AsyncPostBG extends AsyncTask<String, String, String> { private ProxyState mData = null; private String mName = null; pub

我想升级发布json的代码。 第一个实现了HttpClient,它工作得很好! 我尝试使用HttpURLconnection使用新的实现,但它不起作用!我不能发送任何邮寄请求。 我错过了什么

public class AsyncPostBG extends AsyncTask<String, String, String> {

    private ProxyState mData = null;
    private String mName = null;

    public AsyncPostBG(ProxyState data, String name)
    {
        mData = data;
        mName = name;
    }

    @Override
    protected String doInBackground(String... params){
        HttpParams httpParams = new BasicHttpParams();
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        HttpPost postMethod = new HttpPost(params[0]);
        postMethod.setHeader("Content-Type", "application/json; charset=utf-8");
        postMethod.setHeader("Accept", "*/*");
        postMethod.setHeader("Accept-Encoding", "gzip, deflate");
        Gson gson=new Gson();
        String json = gson.toJson(mData);
        try {
            postMethod.setEntity(new ByteArrayEntity(json.getBytes("UTF8")));
            HttpResponse response = httpClient.execute(postMethod);
            StatusLine statusLine = response.getStatusLine();
        }
        catch (UnsupportedEncodingException e){

        }
        catch (Exception e) {
        }
        return json;
    }
}
公共类AsyncPostBG扩展了AsyncTask{
私有代理状态mData=null;
私有字符串mName=null;
公共AsyncPostBG(代理状态数据,字符串名称)
{
mData=数据;
mName=名称;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpParams HttpParams=新的BasicHttpParams();
HttpClient HttpClient=新的默认HttpClient(httpParams);
HttpPost postMethod=新的HttpPost(参数[0]);
setHeader(“内容类型”,“应用程序/json;字符集=utf-8”);
postMethod.setHeader(“接受”,“*/*”);
setHeader(“接受编码”、“gzip、deflate”);
Gson Gson=新的Gson();
字符串json=gson.toJson(mData);
试一试{
setEntity(新的ByteArrayEntity(json.getBytes(“UTF8”));
HttpResponse response=httpClient.execute(postMethod);
StatusLine StatusLine=response.getStatusLine();
}
捕获(不支持的编码异常e){
}
捕获(例外e){
}
返回json;
}
}
以下是我的HttpURLConnection实现:

private class AsyncStatePostBG extends AsyncTask<String, Void,  String> {
    private ProxyObj mData = null;
    private String mName = null;

    public AsyncStatePostBG(ProxyObj data, String name) {
        mData = data;
        mName = name;
    }

    @Override
    protected String doInBackground(String... params) {
        Gson gson = new Gson();
        String json = gson.toJson(mData);

        HttpURLConnection connection = null;
        try {
            URL object = new URL(ComURL + "api/state/" + mName);
            connection = (HttpURLConnection) object.openConnection();

            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "*/*");
            connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
            connection.connect();
            OutputStreamWriter streamWriter = new OutputStreamWriter(connection.getOutputStream());
            streamWriter.write(json);
            streamWriter.flush();
            streamWriter.close();

        } catch (Exception exception) {

            return null;
        }
        return json;

    }
}
私有类AsyncStatePostBG扩展了AsyncTask{
私有ProxyObj mData=null;
私有字符串mName=null;
公共AsyncStatePostBG(ProxyObj数据,字符串名称){
mData=数据;
mName=名称;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
Gson Gson=新的Gson();
字符串json=gson.toJson(mData);
HttpURLConnection=null;
试一试{
URL对象=新URL(ComURL+“api/state/”+mName);
connection=(HttpURLConnection)对象。openConnection();
connection.setDoOutput(真);
connection.setDoInput(true);
connection.setRequestMethod(“POST”);
setRequestProperty(“内容类型”、“应用程序/json”);
connection.setRequestProperty(“接受”,“*/*”);
setRequestProperty(“接受编码”、“gzip、deflate”);
connection.connect();
OutputStreamWriter streamWriter=新的OutputStreamWriter(connection.getOutputStream());
streamWriter.write(json);
streamWriter.flush();
streamWriter.close();
}捕获(异常){
返回null;
}
返回json;
}
}

你只要用这个代码就行了,它对我很有用

     URL url = new URL("Your URL");
     HttpURLConnection httpsURLConnection = (HttpURLConnection)url.openConnection();
     httpsURLConnection.setReadTimeout(15000);
     httpsURLConnection.setConnectTimeout(20000);
     httpsURLConnection.setDoInput(true);
     httpsURLConnection.setRequestProperty("Content-Type", "application/json");
//Method Type
     httpsURLConnection.setRequestMethod("POST" : "PUT");
     OutputStream outputStream = httpsURLConnection.getOutputStream();
     BufferedWriter bufferedWriter = new BufferedWriter(new   OutputStreamWriter(outputStream, "UTF-8"));
     bufferedWriter.write("Your Params");
     bufferedWriter.flush();
     bufferedWriter.close();
     outputStream.close();
     httpsURLConnection.connect();    
     int mStatus = httpsURLConnection.getResponseCode();
检查响应代码,得到如下结果

if (mStatus == 200 || mStatus == 201)
 return readResponse(httpsURLConnection.getInputStream()).toString();
获取响应的方法

private static StringBuilder readResponse(InputStream inputStream) throws IOException, NullPointerException {
        BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            stringBuilder.append(line);
        }
        return stringBuilder;
    }

“它不起作用”的确切意思是什么?我无法收到任何可以帮助你的请求。你默默地接受所有例外。但是,它们为诊断问题提供了有价值的输入。