Android上的HTTP POST到PHP引发空指针错误
我想用一些参数(名称、电子邮件、密码等)对php脚本进行一个简单的HTTPRequest,以进行用户注册。我试过很多网上的教程,但都不适合我。HTTP POST抛出Android上的HTTP POST到PHP引发空指针错误,php,android,json,android-json,Php,Android,Json,Android Json,我想用一些参数(名称、电子邮件、密码等)对php脚本进行一个简单的HTTPRequest,以进行用户注册。我试过很多网上的教程,但都不适合我。HTTP POST抛出NullpointerException。在后端,它也显示null 我的代码是: //JSON Node Names private static final String TAG_ID = "status"; private static final String TAG_NAME
NullpointerException
。在后端,它也显示null
我的代码是:
//JSON Node Names
private static final String TAG_ID = "status";
private static final String TAG_NAME = "code";
private static final String TAG_EMAIL = "message";
//POST data
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("user_name", "Steve"));
nameValuePairs.add(new BasicNameValuePair("user_email", "jjdnjdn"));
nameValuePairs.add(new BasicNameValuePair("user_password", "dcds"));
nameValuePairs.add(new BasicNameValuePair("user_phone_number", "2343"));
nameValuePairs.add(new BasicNameValuePair("club_member_id", "24"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//For receiving response
// Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
Log.e("Data", String.valueOf(json));
try {
String id = json.getString(TAG_ID);
String name = json.getString(TAG_NAME);
String email = json.getString(TAG_EMAIL);
Toast.makeText(getContext(), id , Toast.LENGTH_SHORT).show();
Toast.makeText(getContext(), name , Toast.LENGTH_SHORT).show();
Toast.makeText(getContext(), email , Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
PHP代码
<?php
print_r($_POST);
?>
获取空数组。jParser.getJSONFromUrl(url)似乎是GET而不是POST(而且无论如何,您没有使用httppost,也没有从jParser引用它,因此它无法肯定地发布您的数据
在th ephp中,您可能还需要在回显json正文之前发送一个正确的内容类型头,因为jParser可能依赖于它。尝试以下方法:
应用程序端
private static final String TAG_ID = "status";
private static final String TAG_NAME = "code";
private static final String TAG_EMAIL = "message";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("user_name", "Steve"));
nameValuePairs.add(new BasicNameValuePair("user_email", "jjdnjdn"));
nameValuePairs.add(new BasicNameValuePair("user_password", "dcds"));
nameValuePairs.add(new BasicNameValuePair("user_phone_number", "2343"));
nameValuePairs.add(new BasicNameValuePair("club_member_id", "24"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200){
JSONObject jsonResponse = new JSONObject(response);
String id = jsonResponse .getString(TAG_ID);
String name = jsonResponse .getString(TAG_NAME);
String email = jsonResponse .getString(TAG_EMAIL);
}
} catch (JSONException e) {
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
<?php
file_put_contents(__DIR__."/request_log.txt", "Request : ".json_encode($_REQUEST));
if(!empty($_REQUEST))
{
$user_name = $_REQUEST["user_name"];
$user_email = $_REQUEST["user_email"];
$user_password = $_REQUEST["user_password"];
$user_phone_number = $_REQUEST["user_phone_number"];
$club_member_id = $_REQUEST["club_member_id"];
/*
Add your code
*/
$result = array("status" => "status_value", "code" => "code_value", "message" => "message");
file_put_contents(__DIR__."/request_log.txt", "Result : ".json_encode($result), FILE_APPEND);
echo json_encode($result);
}
?>
private static final String TAG_ID=“status”;
私有静态最终字符串标记\u NAME=“code”;
私有静态最终字符串标记\u EMAIL=“message”;
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“用户名”、“史蒂夫”);
添加(新的BasicNameValuePair(“用户电子邮件”,“jjdnjdn”);
添加(新的BasicNameValuePair(“用户密码”、“dcds”);
添加(新的BasicNameValuePair(“用户电话号码”,“2343”);
名称价值对。添加(新的基本名称价值对(“俱乐部成员id”,“24”));
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(URL);
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200){
JSONObject jsonResponse=新的JSONObject(响应);
String id=jsonResponse.getString(TAG_id);
String name=jsonResponse.getString(标记名);
String email=jsonResponse.getString(TAG_email);
}
}捕获(JSONException e){
}捕获(不支持的编码异常e){
}捕获(IOE异常){
}
服务器端
private static final String TAG_ID = "status";
private static final String TAG_NAME = "code";
private static final String TAG_EMAIL = "message";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("user_name", "Steve"));
nameValuePairs.add(new BasicNameValuePair("user_email", "jjdnjdn"));
nameValuePairs.add(new BasicNameValuePair("user_password", "dcds"));
nameValuePairs.add(new BasicNameValuePair("user_phone_number", "2343"));
nameValuePairs.add(new BasicNameValuePair("club_member_id", "24"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200){
JSONObject jsonResponse = new JSONObject(response);
String id = jsonResponse .getString(TAG_ID);
String name = jsonResponse .getString(TAG_NAME);
String email = jsonResponse .getString(TAG_EMAIL);
}
} catch (JSONException e) {
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
<?php
file_put_contents(__DIR__."/request_log.txt", "Request : ".json_encode($_REQUEST));
if(!empty($_REQUEST))
{
$user_name = $_REQUEST["user_name"];
$user_email = $_REQUEST["user_email"];
$user_password = $_REQUEST["user_password"];
$user_phone_number = $_REQUEST["user_phone_number"];
$club_member_id = $_REQUEST["club_member_id"];
/*
Add your code
*/
$result = array("status" => "status_value", "code" => "code_value", "message" => "message");
file_put_contents(__DIR__."/request_log.txt", "Result : ".json_encode($result), FILE_APPEND);
echo json_encode($result);
}
?>
您是否尝试在浏览器中调用url?是否可以发布您的urljson@Sree:是的,我做得很好。所以这不是URL的问题。@mohit我尝试过这个echo jsone\u encode($\u POST)
,但会得到一个空数组它在浏览器上显示json吗?那么我怎么能使用Httppost呢?你可以用谷歌搜索“android http POST”并选择了数千个答案中的一个。即使在stackoverflow上,您也将资助至少100个示例。当您将json作为字符串获取时,您可以使用JsonParserlogcat解析它。显示无法在register
中找到POST请求。在PHP中$data=json_decode($\u POST[“data”],true)
是否为空?是否在应用程序端出现任何异常?请记录所有异常,并检查是否出现任何异常。JSONObject.getString()上出现NullpointerException是因为来自服务器的响应为空。请在此之前记录jsonParams.toString()=>ArrayList nameValuePairs=new ArrayList();我已经更新了服务器端脚本。请在从应用程序向服务器发送请求后检查request_log.txt。请对request_log.txt数据进行注释?