Lenny
Lenny

Reputation: 917

Post JSON data and receive response in android

I want to post simple JSON to webservice and receive response but I am getting java.io.IOException : No authentication challenges found error on client.getResponseCode()

I tried this solution but it didn't work for me.

Here is my code:

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();
}

Upvotes: 0

Views: 797

Answers (3)

user3766930
user3766930

Reputation: 5829

Are you sure you have a valid android token on the server site? It's just a random thought, but I had a similar issue some time ago ;)

Upvotes: 0

Arora
Arora

Reputation: 113

try this example to encode username password

  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);

Upvotes: 1

coder-don
coder-don

Reputation: 113

I think you'll have a lot more success if you try using OkHttp, an HTTP client for Android made by Square developers. It adds a layer of abstraction so you don't have to write so much code for a simple POST request. Here is an example of the code for POST request to a server:

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();
}

(I would add this as a comment, but I don't yet have the SO rep)

Upvotes: 0

Related Questions