我嘗試做這個教程Tuturial Part 5和我在Regisert.java一個NullPointerException在與代碼行:NullPointerException異常解析JSON
Log.d("Login attempt", json.toString());
我使用WAMP服務器和我已經保存在我的PHP文件目錄www。這意味着我的字符串變量URL ist
"http://10.0.2.2:80/www/login_tutorial/register.php"
。 Hier ist my log.txt
07-13 19:56:20.266: E/AndroidRuntime(29385): FATAL EXCEPTION: AsyncTask #1
07-13 19:56:20.266: E/AndroidRuntime(29385): java.lang.RuntimeException: An error occured while executing doInBackground()
07-13 19:56:20.266: E/AndroidRuntime(29385): at android.os.AsyncTask$3.done(AsyncTask.java:278)
07-13 19:56:20.266: E/AndroidRuntime(29385): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-13 19:56:20.266: E/AndroidRuntime(29385): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-13 19:56:20.266: E/AndroidRuntime(29385): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-13 19:56:20.266: E/AndroidRuntime(29385): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-13 19:56:20.266: E/AndroidRuntime(29385): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-13 19:56:20.266: E/AndroidRuntime(29385): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-13 19:56:20.266: E/AndroidRuntime(29385): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-13 19:56:20.266: E/AndroidRuntime(29385): at java.lang.Thread.run(Thread.java:856)
07-13 19:56:20.266: E/AndroidRuntime(29385): Caused by: java.lang.NullPointerException
07-13 19:56:20.266: E/AndroidRuntime(29385): at com.example.mysqltest.Register$CreateUser.doInBackground(Register.java:105)
07-13 19:56:20.266: E/AndroidRuntime(29385): at com.example.mysqltest.Register$CreateUser.doInBackground(Register.java:1)
07-13 19:56:20.266: E/AndroidRuntime(29385): at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-13 19:56:20.266: E/AndroidRuntime(29385): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-13 19:56:20.266: E/AndroidRuntime(29385): ... 5 more
有人能告訴我爲什麼它不適合我嗎?
感謝
我把代碼從上面的鏈接,但它的確定我會在這裏再重新發布。
public class Register extends Activity implements OnClickListener {
private EditText user, pass;
private Button mRegister;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://10.0.2.2:80/www/login_tutorial/register.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
user = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
mRegister = (Button) findViewById(R.id.register);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
new CreateUser().execute();
}
@Override
protected void onPause() {
super.onPause();
if (pDialog != null) {
pDialog.dismiss();
}
}
class CreateUser extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Register.this);
pDialog.setMessage("Creating user...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request", "starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
// full json response
Log.d("Login attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("User created", json.toString());
finish();
return json.getString(TAG_MESSAGE);
}
else {
Log.d("Login Failure", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
*/
@Override
protected void onPostExecute(String result) {
if (pDialog != null) {
pDialog.dismiss();
}
if (result != null) {
Toast.makeText(Register.this, result, Toast.LENGTH_LONG).show();
}
}
}
}
的梅索德爲考取JSON是在這裏:
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
// Making HTTP Request
try {
// check for request method
if (method == "POST") {
// request method is POST
// defaultHTTPClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if (method == "GET") {
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer error", "Error converting result" + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
爲什麼它給了我null作爲結果的JSON?
你的變量json爲null。如果您需要更多幫助,請在您分配該變量的位置發佈代碼(以及任何相關代碼以獲取該數據)。 –
什麼是'json'。請發佈相關代碼。 – NINCOMPOOP
請不要將logcat輸出顯示爲屏幕截圖。將其複製並粘貼到問題的代碼塊中。 – Squonk