0

I would to send to a localhost server, json data from mobile device in an Android application. I use a real mobile phone as client for testing my app. I'm using API Rest for sending my data, and Servlet for receiving it.

I have a connection error.My mobile phone doesn't connect to server and I don't know why...

This is the client code:

 public void send(){
    StringBuilder sb = new StringBuilder();  
    String http = "http://10.0.2.2/W7TurismoServer/Servlet";  
    ArrayList<String> mostraArray=show();
    HttpURLConnection urlConnection=null;  
    try {  
        URL url = new URL(http);  
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);   
        urlConnection.setRequestMethod("POST");  
        urlConnection.setUseCaches(false);  
        urlConnection.setConnectTimeout(10000);  
        urlConnection.setReadTimeout(10000);  
        urlConnection.setRequestProperty("Content-Type","application/json");   
        urlConnection.setRequestProperty("Host","http://10.0.2.2/W7TurismoServer/Servlet");

        urlConnection.connect();  

        //Create JSONObject here*/
        JSONObject jsonParam = new JSONObject();
        JSONArray arraypref=new JSONArray();

        arraypref.put(mostraArray);
        jsonParam.put("stringList",arraypref);
        System.out.println(jsonParam);
        OutputStreamWriter out = new   OutputStreamWriter(urlConnection.getOutputStream());
        out.write(jsonParam.toString());
        out.close();  

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

            System.out.println(""+sb.toString());  

        }else{  
                System.out.println(urlConnection.getResponseMessage());  
        }  
    } catch (MalformedURLException e) {  

             e.printStackTrace();  
    }  
    catch (IOException e) {  

        e.printStackTrace();  
        } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{  
        if(urlConnection!=null)  
        urlConnection.disconnect();  
    }  
}

show() method return ArrayList and I would send this array to server.

this is the server code:

 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

    PrintWriter out= response.getWriter();
    out.println("sono11");

    System.out.println("stouk do");
          StringBuffer jb = new StringBuffer();
          String line = null;
          try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null)
              jb.append(line);
          } catch (Exception e) { /*report an error*/ }

          try {

            JSONObject jsonObject = JSONObject.fromObject(jb.toString());
            System.out.println("sono venuto,"+jsonObject);
          } catch (ParseException e) {
            // crash and burn
            throw new IOException("Error parsing JSON request string");
          }

}

}

This class is in W7TurismoServer. Thank's in advance for helping.

1 Answers1

0

It is most likely a NetworkOnMainThreadException, which tells you that you are doing a HTTP Connection on UI thread, and because it can cause unresponsiveness, is discouraged.

The Quick Fix is already answered in other StackOverflow Thread, but for a better fix, you will need to use an AsyncTask.

Community
  • 1
  • 1
reidzeibel
  • 1,622
  • 1
  • 19
  • 24