这是A类的函数,我将我的值传递给类名确认其寄存器函数.
private void registerOrder() {
confirm.register(id);
}
这是课堂确认.在这个课程中,我计划在服务器内发布数据.网址没有错误.我打印了这段代码“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();
}
}
最佳答案 您有一个实例变量,它永远不会分配给:
ProgressDialog loading;
它用于
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss(); // expect NPE
}