无法将JSONObject从android发布到PHP
我试图使用下面的代码将JSONObject从android应用程序发布到php web应用程序无法将JSONObject从android发布到PHP,php,android,Php,Android,我试图使用下面的代码将JSONObject从android应用程序发布到php web应用程序 String url = "http://10.0.2.2/test/" URL rest_url = new URL(url); JSONObject params = new JSONObject(); params.put("username","admin"); // Send POST data request BufferedReader reader=null; DataOutpu
String url = "http://10.0.2.2/test/"
URL rest_url = new URL(url);
JSONObject params = new JSONObject();
params.put("username","admin");
// Send POST data request
BufferedReader reader=null;
DataOutputStream printout;
URLConnection conn = rest_url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( params.toString());
wr.close();
当我试图用php检索用户名时,得到的是空值
<?php echo $_POST['username'];
?>
您没有在Android代码上发送
POST
请求。您只是在打印一个普通的字符串
,在您的示例中,它就是您的JSONObject
。你必须改变一些东西,它才能工作
首先,将您的conn
设置为发送POST
conn.setRequestMethod("POST");
然后,创建名称值对
的列表
。就像下面的例子
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username", params.toString()));
编辑:似乎API 22上不推荐使用
NameValuePair
,请查看更多详细信息。您没有在Android代码上发送POST
请求。您只是在打印一个普通的字符串
,在您的示例中,它就是您的JSONObject
。你必须改变一些东西,它才能工作
首先,将您的conn
设置为发送POST
conn.setRequestMethod("POST");
然后,创建名称值对
的列表
。就像下面的例子
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username", params.toString()));
编辑:似乎API 22上不推荐使用
NameValuePair
,请查看更多详细信息。为了避免在每个类中为连接编写HTTP代码,我这样做了
创建了单独的http类,并放置了以下代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.util.Log;
public class HttpConnect
{
HttpClient httpClient;
String Url;
private static final String DEBUG_TAG = "TAG";
public HttpConnect(String url) {
this.Url = url;
httpClient = new DefaultHttpClient();
}
public HttpEntity Get() throws ClientProtocolException, IOException{
HttpGet httpGet = new HttpGet(Url);
HttpResponse httpResponseGet = httpClient.execute(httpGet);
HttpEntity resEntityGet = httpResponseGet.getEntity();
return resEntityGet;
}
public HttpEntity Post(HashMap<String, String> c) throws ClientProtocolException, IOException{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Url);
// set up post data
ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
Iterator<String> it = c.keySet().iterator();
while (it.hasNext())
{
String key = it.next();
nameValuePair.add(new BasicNameValuePair(key, c.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
HttpResponse httpResponseGet = httpClient.execute(httpPost);
HttpEntity resEntityGet = httpResponseGet.getEntity();
return resEntityGet;
}
}
将resEntityGet
entitiy转换为字符串,如下所示
String responseString = EntityUtils.toString(resEntityGet); //response returned from your script
为了避免在每个类中为连接编写HTTP代码,我这样做了 创建了单独的http类,并放置了以下代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.util.Log;
public class HttpConnect
{
HttpClient httpClient;
String Url;
private static final String DEBUG_TAG = "TAG";
public HttpConnect(String url) {
this.Url = url;
httpClient = new DefaultHttpClient();
}
public HttpEntity Get() throws ClientProtocolException, IOException{
HttpGet httpGet = new HttpGet(Url);
HttpResponse httpResponseGet = httpClient.execute(httpGet);
HttpEntity resEntityGet = httpResponseGet.getEntity();
return resEntityGet;
}
public HttpEntity Post(HashMap<String, String> c) throws ClientProtocolException, IOException{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Url);
// set up post data
ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
Iterator<String> it = c.keySet().iterator();
while (it.hasNext())
{
String key = it.next();
nameValuePair.add(new BasicNameValuePair(key, c.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
HttpResponse httpResponseGet = httpClient.execute(httpPost);
HttpEntity resEntityGet = httpResponseGet.getEntity();
return resEntityGet;
}
}
将resEntityGet
entitiy转换为字符串,如下所示
String responseString = EntityUtils.toString(resEntityGet); //response returned from your script
这也是一个很好的解决方案。但据我所知,apache http客户端不再受支持,它也是一个很好的解决方案。但据我所知,apache http客户端不再受支持。