在我的应用程序中,我想通过 httpPost 中的 url 传递一个 json 参数。我的网址是
http:\\xyz\login.php?
我有 json 参数{"userName"="ekant","password"="xyz"}
我希望这个 json 像http:\\xyz\login.php?userName=ekant&password=xyz
查询字符串一样与 url 一起传递。谁能告诉我该怎么做?谢谢
你可以使用这个类,我在很多应用程序中都使用过它。
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();
}
}
要使用这个...
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();
希望能帮助到你...
您可以使用 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()));
}
}));
使用名称值对
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.首先将java对象转换为json
JSONObject json = new JSONObject();
json.put("userName", youvalue);
json.put("password", yourvlaue);
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
}
注意:-此代码未经测试...
如果你使用 jquery,很容易 slove data = {"userName"="ekant","password"="xyz"}; $.param(数据)。