[英]How to pass a json parameteter with an url in httpPost in android
In my app I want to pass a json parameter with the url in httpPost.在我的应用程序中,我想通过 httpPost 中的 url 传递一个 json 参数。 My Url is
我的网址是
http:\\xyz\login.php?
and I have the json parameter like {"userName"="ekant","password"="xyz"}
我有像
{"userName"="ekant","password"="xyz"}
这样的 json 参数
I want this json to pass with the url like http:\\\\xyz\\login.php?userName=ekant&password=xyz
as a query string.我希望这个 json 将像
http:\\\\xyz\\login.php?userName=ekant&password=xyz
这样的 url 作为查询字符串http:\\\\xyz\\login.php?userName=ekant&password=xyz
。 Can anybody tell me how to do this?谁能告诉我如何做到这一点? Thanx
谢谢
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;
}
1.First convert java object to json 1.首先将java对象转换为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 2.然后将json对象作为参数传递到您的post方法中
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 ...注意:-此代码未经测试...
if you are use jquery,It's easy to slove data = {"userName"="ekant","password"="xyz"};如果你使用jquery,很容易slove data = {"userName"="ekant","password"="xyz"}; $.param(data).
$.param(数据)。
You can use droidQuery to send this very easily:您可以使用 droidQuery 非常轻松地发送此信息:
$.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()));
}
}));
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...希望它有帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.