简体   繁体   中英

Error :java.io.FileNotFoundException

In my project I am posting json values to my server using the http POST method. But while posting I am getting this error message:

W/System.err: java.io.FileNotFoundException: http://10.1.7.95:2403/beacons
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
W/System.err:     at com.beaconsapptwo.estimote.NetworkMgr.callPostMethod(NetworkMgr.java:110)
W/System.err:     at com.beaconsapptwo.MainActivity$2.onClick(MainActivity.java:235)
W/System.err:     at android.view.View.performClick(View.java:5204)
W/System.err:     at android.view.View$PerformClick.run(View.java:21153)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

My PostMethod is this:

 public static String callPostMethod(String urlString ,String Json){
 Log.d(TAG,"URL going to call " + urlString + ":::"+Json );
  HttpURLConnection httpcon;
  URL url = null;
  String result = null;
  try{

  url = new URL(urlString.toString());
  httpcon = (HttpURLConnection) url.openConnection();
  httpcon.setDoOutput(true);
  httpcon.setRequestProperty("Content-Type", "application/json");
  httpcon.setRequestProperty("Accept", "application/json");
  httpcon.setRequestMethod("POST");
  httpcon.connect();

  //Write         
  OutputStream os = httpcon.getOutputStream();
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
  writer.write(Json);
  writer.close();
  os.close();

  //Read      
  BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream(),"UTF-8"));

  String line = null; 
  StringBuilder sb = new StringBuilder();         

  while ((line = br.readLine()) != null) {  
       sb.append(line); 
  }       

  br.close();  
  result = sb.toString();

  } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  } 
    return result;
 }

This is the Json data:

    {
    "uuid": "B9402F30-F5F8-466E-AFI9-25556B57FE6D",
    "minor": "43406",
    "major": "23236",
    "title": "Virtual Beacon",
    "message": "Virtual Beacon message"
    }

Where I'm wrong?

You never set your HttpURLConnection to allow input. Try setting httpcon.setDoInput(true); in your HttpURLConnection settings.

Also, you might try removing the line httpcon.setRequestMethod("POST"); Calling httpcon.setDoOutput(true); already defaults your connection method to POST, and is therefore unneeded. Plus, setRequestMethod() is used for other settings according to the docs:

HttpURLConnection uses the GET method by default. It will use POST if setDoOutput(true) has been called. Other HTTP methods (OPTIONS, HEAD, PUT, DELETE and TRACE) can be used with setRequestMethod(String).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM