Java Android JSON通信

Java Android JSON通信,java,php,android,json,web-services,Java,Php,Android,Json,Web Services,我想从Android设备在PHP服务器上执行此查询: { "command": "REGISTER", "data": { "email": "EMAIL", "login": "LOGIN", "password": "PASSWORD", "language": "USER_LANGUAGE" } 公共类AysnChrousTaskPost扩展异步任务{ 私有静态最终字符串路径=”http:

我想从Android设备在PHP服务器上执行此查询:

{
    "command": "REGISTER",
    "data": {
        "email": "EMAIL",
        "login": "LOGIN",
        "password": "PASSWORD",
        "language": "USER_LANGUAGE"      
    }
公共类AysnChrousTaskPost扩展异步任务{
私有静态最终字符串路径=”http://alphabravo.com/myapp/api.php";
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
输入流mInputStream;
字符串lineString=“”;
字符串resultString=“”;
StringBuilder mStringBuilder=null;
List nameValuePairList=新的ArrayList();
nameValuePairList.add(新的BasicNameValuePair(“命令”、“寄存器”);
nameValuePairList.add(新的BasicNameValuePair(“电子邮件”)abc@gmail.com"));
nameValuePairList.add(新的BasicNameValuePair(“登录”,“abc”));
nameValuePairList.add(新的BasicNameValuePair(“密码”,“1205”));
nameValuePairList.add(新的BasicNameValuePair(“语言”,“英语”));
试一试{
HttpClient mHttpClient=新的默认HttpClient();
HttpPost mhttpost=新的HttpPost(路径);
mhttpost.setEntity(新的UrlEncodedFormEntity(nameValuePairList));
HttpResponse mHttpResponse=mHttpClient.execute(mHttpPost);
HttpEntity mHttpEntity=mHttpResponse.getEntity();
StatusLine mStatusLine=mHttpResponse.getStatusLine();
int statusCodeString=mStatusLine.getStatusCode();
如果(statusCodeString==200){
mInputStream=mhttpenty.getContent();
mStringBuilder=新的StringBuilder();
BufferedReader mBufferedReader=新的BufferedReader(
新的InputStreamReader(mInputStream,“iso-8859-1”),8);
而((lineString=mBufferedReader.readLine())!=null)
mStringBuilder.append(lineString+“/n”);
}
resultString=mStringBuilder.toString();
}捕获(例外e){
//TODO:处理异常
}
返回结果字符串;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
}
我得到了一个空的
BufferedReader.ReadLine()
。我是否正确创建了
mhttpost
? 我还尝试了
HTTPget
方法。 但结果是一样的,请帮帮我


我已在浏览器上检查了我的php脚本。我的php脚本工作正常。

您需要将键值对转换为内容类型应用程序/json,然后将其发送到服务器

在asynctask中尝试此操作

JSONObject data = new JSONObject();

// Add key/value pairs
data.put("email", email);
data.put("login", login);
data.put("password", password);
data.put("language", language);

JSONObject json = new JSONObject();
json.put("command", command);
json.put("data", data);

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost("http://alphabravo.com/myapp/api.php");

StringEntity se;
se = new StringEntity(json.toString());

// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");


HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);

HttpEntity entity = response.getEntity();

String receiveJson = EntityUtils.toString(entity);

//use receiveJson as new JSONObject(receiveJson) ot new JSONArray(receiveJson) as per your returned json value

您是否测试过以确保php脚本返回正确的json值?是的,我的php脚本返回的值正确,我已经检查过这一点尝试记录
mInputStream
的结果请注意,如果您试图在服务器端获取
json
对象,您将得不到,因为
mhtppost.setEntity()
httppost
parameters not
json
representation的形式发送您的参数然后如何使用这个'JSONObject postJsonObject=new JSONObject()发送json参数并获得json响应谢谢,我已经试过了,但是“receiveJSON”仍然是空的,我希望JSON对象是空的return@kami:我怀疑您的php脚本是否没有返回任何内容,只需在php中打印传入的json并放入
Log.d(“receiveJson”,receiveJson)
,看看它是否会出现在日志中。@kami:你能在服务器端用PHP告诉我你是如何获取和解析android发送的json的吗。因为现在问题出在服务器端,我想,因为上面的代码工作得很好,我在2-3个项目中使用过它,没有任何问题。
JSONObject data = new JSONObject();

// Add key/value pairs
data.put("email", email);
data.put("login", login);
data.put("password", password);
data.put("language", language);

JSONObject json = new JSONObject();
json.put("command", command);
json.put("data", data);

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost("http://alphabravo.com/myapp/api.php");

StringEntity se;
se = new StringEntity(json.toString());

// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");


HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);

HttpEntity entity = response.getEntity();

String receiveJson = EntityUtils.toString(entity);

//use receiveJson as new JSONObject(receiveJson) ot new JSONArray(receiveJson) as per your returned json value