【问题标题】:Post JSON data and receive response in android在android中发布JSON数据并接收响应
【发布时间】:2016-03-05 22:13:19
【问题描述】:

我想将简单的 JSON 发布到 Web 服务并接收响应,但我在 client.getResponseCode() 上收到 java.io.IOException : No authentication challenges found 错误

我尝试了this solution,但它对我不起作用。

这是我的代码:

StringBuilder sb = new StringBuilder();
HttpURLConnection client = null;

try {
     URL url = new URL("http://myurl:3000");
     client = (HttpURLConnection) url.openConnection();
     client.setDoOutput(true);
     client.setDoInput(true);
     client.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
     client.setRequestProperty("Authorization", "Basic " + Base64.encodeToString("userid:pwd".getBytes(), Base64.NO_WRAP));
     client.setRequestMethod("POST");

     client.connect();

     OutputStreamWriter writer = new OutputStreamWriter(client.getOutputStream());
     String output = json.toString();
     writer.write(output);
     writer.flush();
     writer.close();

     int HttpResult = client.getResponseCode();
     if (HttpResult == HttpURLConnection.HTTP_OK) {
         BufferedReader br = new BufferedReader(new InputStreamReader(
                            client.getInputStream(), "utf-8"));
         String line = null;
         while ((line = br.readLine()) != null) {
               sb.append(line + "\n");
         }
         br.close();
     }
} catch (IOException e) {
  this.e = e;
} finally {
  client.disconnect();
}

【问题讨论】:

    标签: android json post httpurlconnection


    【解决方案1】:

    试试这个例子来编码用户名密码

      username = mUsername.getText().toString();
                password = mPassword.getText().toString();
                mResult.setText(username + ":" + password);
                mCombination = username + ":" + password;
                byte[] byteArray = new byte[0];
                try {
                    byteArray = mCombination.getBytes("UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
    
                final String base64 = Base64.encodeToString(byteArray, Base64.NO_WRAP);
                mEncoded.setText(base64);
    

    【讨论】:

      【解决方案2】:

      您确定您在服务器站点上有一个有效的 android 令牌吗?这只是一个随机的想法,但我前段时间遇到了类似的问题;)

      【讨论】:

      • 我不敢相信——我的代码一直都没有问题,但问题是我来自谷歌的 server_client_id 在网络服务上是错误的。你为我节省了很多工作!谢谢
      【解决方案3】:

      我认为,如果您尝试使用由 Square 开发人员开发的用于 Android 的 HTTP 客户端 OkHttp,您将获得更大的成功。它增加了一层抽象,因此您不必为简单的 POST 请求编写太多代码。以下是向服务器发出 POST 请求的代码示例:

      public static final MediaType JSON
          = MediaType.parse("application/json; charset=utf-8");
      
      OkHttpClient client = new OkHttpClient();
      
      String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
      }
      

      (我会将此添加为评论,但我还没有 SO 代表)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-22
        • 1970-01-01
        • 2012-01-20
        • 2017-07-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多