Android 到 PHP 上的 HTTP POST 抛出 NullPointer 错误
HTTP POST on Android to PHP throws NullPointer Error
我想向 php 脚本发送一个简单的 HTTPRequest,用于使用一些参数(姓名、电子邮件、密码等)进行用户注册。我尝试了很多来自互联网的教程,但对我没有任何用处。 HTTP POST 抛出 NullpointerException
。在后端也显示为空。
我的代码是:
//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();
}
}
Logcat 错误
E/JSON Parser: Error parsing data org.json.JSONException: Value null of type org.json.JSONObject cannot be converted to JSONObject
E/Data: null
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
PHP代码
<?php
print_r($_POST);
?>
获取空数组。
jParser.getJSONFromUrl(url) 似乎是 GET 而不是 POST(无论如何你没有使用 httppost 或从 jParser 引用它,所以它不能 POST 肯定是你的数据。
在 ephp 中,您可能还需要在回显 json body 之前发送正确的 Content-type header,因为 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);
}
?>
我想向 php 脚本发送一个简单的 HTTPRequest,用于使用一些参数(姓名、电子邮件、密码等)进行用户注册。我尝试了很多来自互联网的教程,但对我没有任何用处。 HTTP POST 抛出 NullpointerException
。在后端也显示为空。
我的代码是:
//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();
}
}
Logcat 错误
E/JSON Parser: Error parsing data org.json.JSONException: Value null of type org.json.JSONObject cannot be converted to JSONObject
E/Data: null
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
PHP代码
<?php
print_r($_POST);
?>
获取空数组。
jParser.getJSONFromUrl(url) 似乎是 GET 而不是 POST(无论如何你没有使用 httppost 或从 jParser 引用它,所以它不能 POST 肯定是你的数据。
在 ephp 中,您可能还需要在回显 json body 之前发送正确的 Content-type header,因为 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);
}
?>