从android向PHP MySql插入6列,显示这3个错误 使用Fragment实现App /代码,此代码在其中一个片段文件上完成。 在Manifest中也添加了Internet权限
Log Cat
06-23 00:52:53.830: E/Fail 1(1512):android.os.NetworkOnMainThreadException
06-23 00:52:53.830: E/Fail 2(1512):java.lang.NullPointerException:lock==null
06-23 00:52:53.830: E/Fail 3(1512):java.lang.NullPointerException
Android代码
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addque = question.getText().toString();
addc1 = choice1.getText().toString();
addc2 = choice2.getText().toString();
addc3 = choice3.getText().toString();
addanswer = answer.getText().toString();
addexplan = Explanation.getText().toString();
insert();
//Toast.makeText(getActivity(), "Question posted", Toast.LENGTH_SHORT).show();
}
});
将函数插入同一文件
public void insert() {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("addque", addque));
nameValuePairs.add(new BasicNameValuePair("addc1", addc1));
nameValuePairs.add(new BasicNameValuePair("addc2", addc2));
nameValuePairs.add(new BasicNameValuePair("addc3", addc3));
nameValuePairs.add(new BasicNameValuePair("addanswer", addanswer));
nameValuePairs.add(new BasicNameValuePair("addexplan", addexplan));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://localhost/insert/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
} catch (Exception e) {
Log.e("Fail 1", e.toString());
//Toast.makeText(getApplicationContext(), "Invalid IP
Address ",Toast.LENGTH_LONG).show();
}
try {
BufferedReader reader = new BufferedReader
(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
} catch (Exception e) {
Log.e("Fail 2", e.toString());
}
try {
JSONObject json_data = new JSONObject(result);
code = (json_data.getInt("code"));
if (code == 1) {
Toast.makeText(getActivity(), "Inserted Successfully",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.e("Fail 3", e.toString());
}
}
Android中从Mysql中插入数据的新功能 三江源
答案 0 :(得分:1)
Android不允许在UI线程上进行任何网络操作,您需要创建单独的线程来执行此操作。您可以使用asynctask,如下所示:
class MyAsyncTask extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... params) {
insert();
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
您的stacktrace清楚地表明您正在从UI线程调用网络API。您应该使用后台线程或AsyncTask。
答案 3 :(得分:0)
对于NetworkOnMainThreadException,你需要在后台线程上移动插入调用,这是由于
您无法在主线程上调用网络操作,您必须创建执行操作的后台线程。
,为此您需要使用AsyncTask
AsyncTask.execute(new Runnable() {
@Override
public void run() {
insert(); // call insert method in this block
// which is background thread,
// prevents network call to run on MainThread
}
});
并且如果需要在插入方法调用的主线程上运行任何更新,runOnUiThread就像Toast一样,提醒。等
runOnUiThread(new Runnable() {
@Override
public void run() {
// Toast, Alerts or any operation that needs
// to interact on UI, call here
}
});
如果您正在使用片段调用
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// Toast, Alerts or any operation that needs
// to interact on UI, call here
}
});
PS:在实施之前,您需要清楚地了解这些主题,因此您必须浏览开发人员文档。