1

I am trying to send a simple json object to a server. I can send a json object to the server using postman without any issue. I am using the code from correct answer by Tom Alabaster from the following link: How To Send json Object to the server from my android app

But getting File not found error (giving my server name) in the following line: DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());

The server is working without any issue through Postman. I would like to analyze the issue deeper. How I can capture any helpful error code?

Error code:
08-24 18:21:21.193 17299-20507/com.tulga.nar.mytracker I/System.out: 
(HTTPLog)-Static: isSBSettingEnabled false
08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err: 
java.io.FileNotFoundException: https://myserver....../mydatabase
08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err:   
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConn
ectionImpl.java:242)
08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err:   at 

com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) 08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25) 08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err: at com.tulga.nar.mytracker.MainActivity$SendDeviceDetails.doInBackground(MainActivity.java:58) 08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err: at com.tulga.nar.mytracker.MainActivity$SendDeviceDetails.doInBackground(MainActivity.java:33) 08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295) 08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) 08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 08-24 18:21:21.623 17299-20507/com.tulga.nar.mytracker W/System.err: at java.lang.Thread.run(Thread.java:818) 08-24 18:21:51.523 17299-17299/com.tulga.nar.mytracker V/ActivityThread: updateVisibility : ActivityRecord{3cf0352 token=android.os.BinderProxy@24d32cf {com.tulga.nar.mytracker/com.tulga.nar.mytracker.MainActivity}} show : true

And Batu
  • 93
  • 2
  • 8

3 Answers3

1

Example for login. Here we are posting username and password for login

        String lname = "";
    try {
        url = new URL(SNFServerURL+SNFAuthResource);
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url.toString());
        post.setHeader("Content-type", "application/json");
        post.setHeader("Accept", "application/json");
        JSONObject object = new JSONObject();
        String message;

        object.put("password", Flags.password);
        object.put("username", Flags.username);
        message = object.toString();
        post.setEntity(new StringEntity(message, "UTF8"));
        HttpResponse response = client.execute(post);
        int statusCode = response.getStatusLine().getStatusCode();
        if (200 == statusCode) {
            String json = EntityUtils.toString(response.getEntity());
            JSONObject result = new JSONObject(json);
            token = result.getString("token");
            TokenActivity.fname = result.getString("firstName");
            TokenActivity.lname = result.getString("lastName");


        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
Jeffy
  • 276
  • 3
  • 9
0

Try using the HTTPUrlConnection as before, but also set the request property like this (if you have not already):

Where url is ur url defined to your server.

JSONObject params = new JSONObject();
    try {
        params.put("some_key_1", value_1);
        params.put("some_key_2", value_2);
    } catch (JSONException | NumberFormatException exception) {
        exception.printStackTrace();
    }

HttpURLConnection connection = (HttpURLConnection) url.getUrl().openConnection();   


        connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoOutput(true);

            osw = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
            // Send request.
            osw.write(params.toString());
            osw.flush();
            osw.close();
Ludvig W
  • 744
  • 8
  • 27
  • Thanks. I will try the evening. This is a side project. – And Batu Aug 24 '17 at 16:07
  • When I access my server on browser, chrome is adding Secure https:// as prefix automatically. Could it cause the issue? I permitted unauthorized access on my server. – And Batu Aug 24 '17 at 23:55
  • I dont think so, as the name implies its for HTTP, try use the HttpsURLConnection. And to answer your question, yeah definitely, that might be a problem. – Ludvig W Aug 25 '17 at 12:13
0
Button submitButton = (Button) findViewById(R.id.submit_button);

submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    JSONObject postData = new JSONObject();
    try {
        postData.put("name", name.getText().toString());
        postData.put("address", address.getText().toString());
        postData.put("manufacturer", manufacturer.getText().toString());
        postData.put("location", location.getText().toString());
        postData.put("type", type.getText().toString());
        postData.put("deviceID", deviceID.getText().toString());

        new SendDeviceDetails.execute("http://52.88.194.67:8080/IOTProjectServer/registerDevice", postData.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
});
private class SendDeviceDetails extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {

    String data = "";

    HttpURLConnection httpURLConnection = null;
    try {

        httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection();
        httpURLConnection.setRequestMethod("POST");

        httpURLConnection.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
        wr.writeBytes("PostData=" + params[1]);
        wr.flush();
        wr.close();

        InputStream in = httpURLConnection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(in);

        int inputStreamData = inputStreamReader.read();
        while (inputStreamData != -1) {
            char current = (char) inputStreamData;
            inputStreamData = inputStreamReader.read();
            data += current;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (httpURLConnection != null) {
            httpURLConnection.disconnect();
        }
    }

    return data;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    Log.e("TAG", result); // this is expecting a response code to be sent from your server upon receiving the POST data
}

}

And Batu
  • 93
  • 2
  • 8