【问题标题】:send data with post method in android在android中使用post方法发送数据
【发布时间】:2017-01-07 10:50:31
【问题描述】:

这是我正在使用的代码:

                HttpClient client = new DefaultHttpClient();
                HttpPost method = new  HttpPost("http://192.168.1.1/value/_0/_0");
                JSONObject object = new JSONObject();
                try {
                    object.put("Val", "0");
                    msg = object.toString();
                    method.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("admin", "00000000"), "UTF-8", false));
                    method.setHeader("Content-Type", "application/json");
                    method.setEntity(new StringEntity(msg, "UTF8"));
                    client.execute(method);
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }

在这里,我添加了用于身份验证目的和请求内容类型的标头。我不知道这有什么问题。

【问题讨论】:

    标签: android json post


    【解决方案1】:

    您需要使用 UrlConnection 并且 DefaultHttpClient() 现在已弃用。

    public String  performPostCall(String requestURL,
        HashMap<String, String> postDataParams) {
    
    URL url;
    String response = "";
    try {
        url = new URL(requestURL);
    
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);
    
    
        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));
    
        writer.flush();
        writer.close();
        os.close();
        int responseCode=conn.getResponseCode();
    
        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line=br.readLine()) != null) {
                response+=line;
            }
        }
        else {
            response="";    
    
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    return response;
    

    }

    【讨论】:

      【解决方案2】:

      这样做:

         public static String postJson(String sUrl, String jsonParams){
         String response = "";
         try {
            HttpURLConnection conn;
            //just encoding space from url
            URL url = new URL(sUrl.replace(" ", "%20"));
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("content-type", "application/json");
      
            byte[] outputInBytes = jsonParams.getBytes("UTF-8");
            OutputStream os = conn.getOutputStream();
            os.write(outputInBytes);
            os.close();
            conn.connect();
      
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
               InputStream inputStream = conn.getInputStream();
               response = convertStreamToString(inputStream);
            }
      
            Log.e("Request: ", sUrl+jsonParams+"");
            Log.e("Response: ", response+"");
         } catch (IOException e) {
            e.printStackTrace();
         }
         return response;
      }
      
      public static String convertStreamToString(InputStream inputStream) {
         if (inputStream == null)
            return null;
         StringBuilder sb = new StringBuilder();
         try {
            BufferedReader r = new BufferedReader(new InputStreamReader(inputStream), 1024);
            String line;
            while ((line = r.readLine()) != null) {
               sb.append(line);
            }
         } catch (Exception e) {
            e.printStackTrace();
         }
         return sb.toString();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-20
        • 2011-02-25
        相关资源
        最近更新 更多