這是類A的功能,我將我的值傳遞給類名確認其註冊函數。即使println有值顯示,爲什麼我會得到空指針異常?
private void registerOrder() {
confirm.register(id);
}
這是等級確認。在這個課上,我有計劃在服務器內發佈數據。網址沒有錯誤。我有println這個代碼「System.out.println(」wei「+ getPostDataString(postDataParams));」我已經獲得了自己的價值。但系統說空指針。其他代碼中是否有錯誤?
public class confirm {
private static final String REGISTER_URL =
"http://192.168.43.214/apexStore2/confirm.php";
public static void register(String id) {
class RegisterUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<>();
data.put("product_id",params[0]);
String result = sendPost(REGISTER_URL,data);
return result;
}
}
RegisterUser ru = new RegisterUser();
ru.execute(String.valueOf(id));
}
public static String sendPost(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
String response = " ";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", "" +
Integer.toString(getPostDataString(postDataParams).getBytes().length));
conn.setRequestProperty("Content-Language", "en-US");
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
System.out.println("wei" + getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br=new BufferedReader(new
InputStreamReader(conn.getInputStream()));
response = br.readLine();
}
else {
response="Error Registering";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private static String getPostDataString(HashMap<String, String> params)
throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}
的println需要消息中使用時,發生當沒有數據在登錄陳述或顯示SOP的。附上一個調試器,並檢查你的價值是否得到。 –
看看從NPE的堆棧跟蹤,看看它從哪裏拋出。 – gmaslowski
進行調試,我應該這樣寫嗎? Log.d(getPostDataString(postDataParams),「onCreate()」); –