0

In my app I want to pass a json parameter with the url in httpPost. My Url is

http:\\xyz\login.php?

and I have the json parameter like {"userName"="ekant","password"="xyz"}

I want this json to pass with the url like http:\\xyz\login.php?userName=ekant&password=xyz as a query string. Can anybody tell me how to do this? Thanx

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ekanta Swain
  • 473
  • 2
  • 13
  • 28
  • see this http://stackoverflow.com/q/18661410/1218762 , http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php ,you can get idea but don't check answer of this question. – Ronak Mehta Oct 17 '13 at 05:14

5 Answers5

0

use namevaluepair

private static String loginURL = "http://www.xyz.com/login.php";

public JSONObject loginUser(String email, String password) {
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("username", name));
    params.add(new BasicNameValuePair("password", password));
    JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
    return json;
}
Dinesh Raj
  • 664
  • 12
  • 30
0

1.First convert java object to json

JSONObject json = new JSONObject();
   json.put("userName", youvalue);
   json.put("password", yourvlaue);

2.Then pass json object as a parameter into your post method

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", json.toString());
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

Note:-This code is not tested ...

QuokMoon
  • 4,387
  • 4
  • 26
  • 50
  • thanx for replying..Can u plz tell me if i ll use namevaluepair json will pass as querry string or somw other format? – Ekanta Swain Oct 17 '13 at 04:45
  • http://developer.android.com/reference/org/apache/http/NameValuePair.html and your json.toString look like this {"userName"="yourvalue","password"="yourvalue"} – QuokMoon Oct 17 '13 at 04:52
0

if you are use jquery,It's easy to slove data = {"userName"="ekant","password"="xyz"}; $.param(data).

shellshy
  • 23
  • 1
  • 4
0

You can use droidQuery to send this very easily:

$.ajax(new AjaxOptions().url("http://www.example.com")//switch with real URL
                        .type("POST")
                        .dataType("json")
                        .data(jsonParams)//your JSON Object
                        .context(this)
                        .success(new Function() {
                            @Override
                            public void invoke($ droidQuery, Object... params) {
                                //if you are expecting a JSONObject:
                                JSONObject response = (JSONObject) params[0];
                                //TODO: handle response
                            }
                        })
                        .error(new Function() {
                            @Override
                            public void invoke($ droidQuery, Object... params) {
                                AjaxError error = (AjaxError) params[0];
                                $.toast(error.status + ": " + error.reason, Toast.LENGTH_LONG);
                                //retry once
                                $.ajax(error.request, error.options.error($.noop()));
                            }
                        }));
0

You can use this class , I have used this in many apps.

package com.your.package;

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<NameValuePair> nameValuePairs;

    public RestPost(String str, List<NameValuePair> 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();
    }
}

To use this...

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("user_id", uid));
pairs.add(new BasicNameValuePair("auth_token", auth));
pairs.add(new BasicNameValuePair("topic_name", TName));

RestPost post = new RestPost(Constants.ForumAddTopic, pairs);
String Response = post.postData();

Hope it helps...

ayon
  • 2,180
  • 2
  • 17
  • 32
  • is the value passes as querry string.I used this but getting erroe reponse.i have to pass as `http:\\xyz\login.php?userName=ekant&password=xyz` – Ekanta Swain Oct 17 '13 at 05:02
  • This code sends request as UrlEncoded form which is exactly what you want. But to make sure that you are correct , send the request with this url "http:\\xyz\login.php?userName=ekant&password=xyz" and with an empty ArrayList of NameValuePair. And if it doesn't work then please let me know exactly what error you are getting. – ayon Oct 17 '13 at 05:15
  • `http://login_mobile.php?username=ekanta.swain@appzoy.com&password=asdf` if i am sendind usernname and password as querrystring as the above url i am getting response.But with this `http://login_mobile.php?` url if i am sending username and password as name valuepair i am getting server side error.like status=false – Ekanta Swain Oct 17 '13 at 05:27
  • I am not getting anything from your url above. In both case. I think your base url that you mentioned is not correct. – ayon Oct 17 '13 at 05:40