title: How can I catch a JSON object sent from an android app to a servlet with doGet method?
body: <p>I want to take username, password &amp; email-id from user, construct a JSON object and send it to a java servlet, which then reads it and insert it into MySql object. I have done using this a php server (source: <a href="http://www.androidhive.info/2011/10/android-login-and-registration-screen-design/" rel="nofollow">http://www.androidhive.info/2011/10/android-login-and-registration-screen-design/</a>) but I need to do this with the help of java servlet. 
  Earlier I was doing this by passing url parameters as follows and it is working properly, but now I want to use the information as JSON parameter:</p>
  
  <p>Android Code:</p>
  
  <pre><code>try {
                  url = new URL("http://10.0.2.2:8080/Servlet/Servlet?param1="
                          + uname + "&amp;param2=" + pwd + "&amp;param3=" + eid);
                  // url = new URL("http://10.0.2.2:8080/Servlet/Servlet");
                  HttpURLConnection urlConnection = (HttpURLConnection) url
                          .openConnection();
                  InputStream in = new BufferedInputStream(
                          urlConnection.getInputStream());
  
                  urlConnection.disconnect();
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
  </code></pre>
  
  <p>Servlet Code:</p>
  
  <pre><code>protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
      {
  
          req.setCharacterEncoding("UTF-8");
          resp.setCharacterEncoding("UTF-8");
          final String uname = req.getParameter("param1");
          final String pwd = req.getParameter("param2");
          final String eid = req.getParameter("param3");
  </code></pre>
  
  <p>I had a look at this (http://stackoverflow.com/questions/11074934/the-json-object-sent-from-android-application-is-null-when-i-want-to-access-him) but couldn't understand.</p>
  
  <p>JSON code is as follows (source: <a href="http://www.androidhive.info/2011/10/android-login-and-registration-screen-design/" rel="nofollow">http://www.androidhive.info/2011/10/android-login-and-registration-screen-design/</a>):</p>
  
  <pre><code>public class JSONParser {
  
      static InputStream is = null;
      static JSONObject jObj = null;
      static String json = "";
  
      // constructor
      public JSONParser() {
  
      }
  
      public JSONObject getJSONFromUrl(String url, List&lt;NameValuePair&gt; params) {
  
          // Making HTTP request
          try {
              // defaultHttpClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(url);
              httpPost.setEntity(new UrlEncodedFormEntity(params));
  
              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
  
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              json = sb.toString();
              Log.e("JSON", json);
          } catch (Exception e) {
              Log.e("Buffer Error", "Error converting result " + e.toString());
          }
  
          // try parse the string to a JSON object
          try {
              jObj = new JSONObject(json);            
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
          }
  
          // return JSON String
          return jObj;
  
      }
  }
  
  
  
  public JSONObject loginUser(String email, String password){
          // Building Parameters
          List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
          params.add(new BasicNameValuePair("tag", login_tag));
          params.add(new BasicNameValuePair("email", email));
          params.add(new BasicNameValuePair("password", password));
          JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
          // return json
          // Log.e("JSON", json.toString());
          return json;
      }
  </code></pre>