Android 使用http、fedup和
我已经使用了大约5种方法将JSON数据发布到HTTP请求URL,但没有收到任何效果,它没有按照我想要的格式发布JSON,下面是我想要发送的JSON,我目前正在使用HTML成功发送: 以下是我想要的JSON: {“x”:“N”,“y”:“55”} 以下是我成功使用的HTML代码:Android 使用http、fedup和,android,json,http,httprequest,Android,Json,Http,Httprequest,我已经使用了大约5种方法将JSON数据发布到HTTP请求URL,但没有收到任何效果,它没有按照我想要的格式发布JSON,下面是我想要发送的JSON,我目前正在使用HTML成功发送: 以下是我想要的JSON: {“x”:“N”,“y”:“55”} 以下是我成功使用的HTML代码: <html><head><script> function sendForm(form) { // Construct the JSON string in form.va
<html><head><script>
function sendForm(form)
{
// Construct the JSON string in form.value
// x is a string ('cos of the quotes)
// y is an integer ('cos of no quotes)
form.value.value = "{ \"x\": \"" + form.example1.value + "\", \"y\": " + form.example2.value + " }"
form.submit();
}
</script></head>
<body>
<form action="https://api.winv.com/v1/4bhj/306adk" method="post">
<input type="hidden" name="value">
string:<input type="text" name="example1">
number:<input type="text" name="example2">
<input type="button" value="Submit" onClick="sendForm(this.form);">
</form>
</body>
</html>
函数sendForm(form)
{
//在form.value中构造JSON字符串
//x是一个字符串('cos of the quotes)
//y是一个整数('无引号的cos)
form.value.value=“{\“x\”:\”+form.example1.value+“\”,“y\”:“+form.example2.value+”}”
表单提交();
}
字符串:
编号:
我试过的代码(都是函数)
public void xyz(){
试一试{
JSONObject json=新的JSONObject();
json.put(“x”,“Nishant”);
json.put(“y”,34567);
HttpParams HttpParams=新的BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
10000);
HttpConnectionParams.setSoTimeout(httpParams,10000);
HttpClient=新的默认HttpClient(httpParams);
//
//字符串url=”http://10.0.2.2:8080/sample1/webservice2.php?" +
//“json={\'UserName\':1,\'FullName\':2}”;
字符串url=“url”;
HttpPost请求=新的HttpPost(url);
setEntity(新的ByteArrayEntity(json.toString().getBytes(
"UTF8);;
setHeader(“json”,json.toString());
HttpResponse response=client.execute(请求);
HttpEntity=response.getEntity();
//如果响应没有包含实体,则不需要
}捕获(可丢弃的t){
Toast.makeText(此“请求失败:”+t.toString(),
Toast.LENGTH_LONG).show();
}
}
public void getServerData()抛出JSONException、ClientProtocolException、,
IOException{
HttpParams HttpParams=新的BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,10000);
HttpConnectionParams.setSoTimeout(httpParams,10000);
HttpClient=新的默认HttpClient(httpParams);
HttpPost请求=新建HttpPost(
“url”);//添加
//你的
//网址
//这里。。。
setHeader(“内容类型”、“应用程序/json”);
JSONObject json=新的JSONObject();
json.put(“y”,“55”);
json.put(“x”,“Nishant”);
Log.i(“jason对象”,json.toString());
StringEntity se=新的StringEntity(json.toString());
se.setContentEncoding(“UTF-8”);
setContentType(“应用程序/json”);
请求。设置实体(se);
HttpResponse response=client.execute(请求);
HttpEntity=response.getEntity();
InputStream=entity.getContent();
字符串_响应=convertStreamToString(is);
System.out.println(“res--”+\u响应);
//检查服务器响应代码是否有效
int res_code=response.getStatusLine().getStatusCode();
System.out.println(“代码--”+res_代码);
}
私有静态字符串convertStreamToString(InputStream为){
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is),
8192);
StringBuilder sb=新的StringBuilder();
字符串行=null;
试一试{
而((line=reader.readLine())!=null){
sb.追加((第+行“\n”);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
使某人返回字符串();
}
public void postData(){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
“网址”);
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“,“Nishant”);
添加(新的BasicNameValuePair(“x:,“45”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
受保护的void sendJson(最终字符串电子邮件,最终整数密码){
线程t=新线程(){
公开募捐{
Looper.prepare();//用于为子级准备消息池
//线
HttpClient=new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),
10000);//超时限制
HttpResponse响应;
JSONObject json=新的JSONObject();
试一试{
HttpPost=新的HttpPost(
“网址”);
json.put(“x”,电子邮件);
json.put(“y”,pwd);
StringEntity se=新的StringEntity(json.toString());
se.setContentType(新的BasicHeader(HTTP.CONTENT\u类型,
“应用程序/json”);
邮政实体(se);
响应=client.execute(post);
/*检查
public void xyz() {
try {
JSONObject json = new JSONObject();
json.put("x", "Nishant");
json.put("y", 34567);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
HttpClient client = new DefaultHttpClient(httpParams);
//
//String url = "http://10.0.2.2:8080/sample1/webservice2.php?" +
// "json={\"UserName\":1,\"FullName\":2}";
String url = "url";
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes(
"UTF8")));
request.setHeader("json", json.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}
public void getServerData() throws JSONException, ClientProtocolException,
IOException {
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost request = new HttpPost(
"url"); // add
// your
// url
// here...
request.setHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("y", "55");
json.put("x", "Nishant");
Log.i("jason Object", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
request.setEntity(se);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String _response = convertStreamToString(is);
System.out.println("res-- " + _response);
// Check if server response is valid code
int res_code = response.getStatusLine().getStatusCode();
System.out.println("code-- " + res_code);
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is),
8192);
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"url");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("", "Nishant"));
nameValuePairs.add(new BasicNameValuePair("x:", "45"));
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
}
}
protected void sendJson(final String email, final int pwd) {
Thread t = new Thread() {
public void run() {
Looper.prepare(); // For Preparing Message Pool for the child
// Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),
10000); // Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try {
HttpPost post = new HttpPost(
"url");
json.put("x", email);
json.put("y", pwd);
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
post.setEntity(se);
response = client.execute(post);
/* Checking response */
if (response != null) {
InputStream in = response.getEntity().getContent(); // Get
// the
// data
// in
// the
// entity
}
} catch (Exception e) {
e.printStackTrace();
// createDialog("Error", "Cannot Estabilish Connection");
}
Looper.loop(); // Loop in the message queue
}
};
StringEntity oStringEntity = new StringEntity( json.toString());
oStringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(oStringEntity);
request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
request.setHeader("json", json.toString());
public static boolean sendJSONtoBLX(String path, JSONObject json)
throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(path);
StringEntity se = new StringEntity(json.toString());
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
@SuppressWarnings("rawtypes")
ResponseHandler responseHandler = new BasicResponseHandler();
@SuppressWarnings("unchecked")
String response = httpclient.execute(httpost, responseHandler);
JSONObject jsonResponse = new JSONObject(response);
String serverResponse = jsonResponse.getString("success");
if (serverResponse.equals("true")) {
return true;
} else {
return false;
}
}
public final static JSONObject writeJSON() throws Exception {
JSONObject obj = new JSONObject();
try {
obj.put("x", "Nishant");
obj.put("y", 34567);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}