title: Sending Json POST to server using name value pair
body: <p>I am  sending Json Post request to server to through url: <a href="http://www.xyz.com/login" rel="nofollow">http://www.xyz.com/login</a></p>
  
  <p><strong>request structure:</strong></p>
  
  <pre><code>{"requestdata":{"password":"abc","devicetype":"phone","username":"amrit@pqr.com","locale":"in"},"requestcode":10}
  </code></pre>
  
  <p>Code Snapshot:</p>
  
  <p><strong>MainActivity:</strong></p>
  
  <pre><code>    // Building post parameters
      // key and value pair
      List&lt;NameValuePair&gt; nameValuePair = new ArrayList&lt;NameValuePair&gt;();
      nameValuePair.add(new BasicNameValuePair("requestcode", "10"));
      nameValuePair.add(new BasicNameValuePair("devicetype", "phone"));
      nameValuePair.add(new BasicNameValuePair("locale", "in"));
      nameValuePair.add(new BasicNameValuePair("username", "amrit@pqr.com"));
      nameValuePair.add(new BasicNameValuePair("password", "abc"));
  
      RestPost post = new RestPost(loginUrl, nameValuePair);
      String Response = post.postData();
      Log.i("Response:", Response);
  </code></pre>
  
  <p><strong>RestPost class</strong></p>
  
  <pre><code>    import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.InputStreamReader;
        import java.util.List;
        import org.apache.http.HttpEntity;
        import org.apache.http.HttpResponse;
        import org.apache.http.NameValuePair;
        import org.apache.http.StatusLine;
        import org.apache.http.client.ClientProtocolException;
        import org.apache.http.client.HttpClient;
        import org.apache.http.client.entity.UrlEncodedFormEntity;
        import org.apache.http.client.methods.HttpPost;
        import org.apache.http.impl.client.DefaultHttpClient;
        import android.util.Log;
  
       public class RestPost {
         String url;
         List&lt;NameValuePair&gt; nameValuePairs;
  
      public RestPost(String str, List&lt;NameValuePair&gt; params) {
          this.url = str;
          this.nameValuePairs = params;
      }
  
      public String postData() {
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(this.url);
          StringBuilder builder = new StringBuilder();
  
          try {
              httppost.setEntity(new UrlEncodedFormEntity(this.nameValuePairs));
              HttpResponse response = httpclient.execute(httppost);
              StatusLine statusLine = response.getStatusLine();
              int statusCode = statusLine.getStatusCode();
              Log.d("RestClient", "Status Code : " + statusCode);
  
              HttpEntity entity = response.getEntity();
              InputStream content = entity.getContent();
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      content));
              String line;
              while ((line = reader.readLine()) != null) {
                  builder.append(line);
              }
  
          } catch (ClientProtocolException e) {
              // TODO Auto-generated catch block
          } catch (IOException e) {
              // TODO Auto-generated catch block
          }
          return builder.toString();
      }
  }
  </code></pre>
  
  <p>But I'm not getting appropriate response, could anyone help me in sending appropriate format for getting server response. Thanks in advance.</p>
  
  <p>Response which I'm getting:</p>
  
  <pre><code>{"error":{"resultCode":"400","status":"Invalid Request format"}}
  </code></pre>