我正在尝试将值插入到android.please中的mysql数据库中,检查下面的代码片段。我的代码没有输入“受保护的字符串在后台执行”。我可以看到进度条然后应用程序停止工作,即之后进入后台做。请帮忙
public class GcmServer extends Activity {
void showToast(CharSequence msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static String url_create_product = "http://192.168.1.4/android/insert_records.php";
private static final String TAG_SUCCESS = "success";
regId = getIntent().getStringExtra("REGID");
userName = getIntent().getStringExtra("USER");
new CreateNewProduct().execute();
} // createNewproduct class
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(GcmServer.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args)
{
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("regId", regId));
params.add(new BasicNameValuePair("name", userName));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
showToast("inserted");
} else {
showToast("not inserted");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
// log cat 01-16 12:01:36.921:E / JSON Parser(2029):解析数据时出错org.json.JSONException:Value(ViewRootImpl.java:345) 01-16 12:01:37.393:E / WindowManager(2029):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239) 01-16 12:01:37.393:E / WindowManager(2029):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 01-16 12:01:37.393:E / WindowManager(2029):在android.app.Dialog.show(Dialog.java:281) 01-16 12:01:37.393:E / WindowManager(2029):at com.example.gcmclientapp.GcmServer $ CreateNewProduct.onPreExecute(GcmServer.java:212) 01-16 12:01:37.393:E / WindowManager(2029):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 01-16 12:01:37.393:E / WindowManager(2029):在android.os.AsyncTask.execute(AsyncTask.java:534) 01-16 12:01:37.393:E / WindowManager(2029):at com.example.gcmclientapp.GcmServer.onCreate(GcmServer.java:99) 01-16 12:01:37.393:E / WindowManager(2029):在android.app.Activity.performCreate(Activity.java:5133) 01-16 12:01:37.393:E / WindowManager(2029):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 01-16 12:01:37.393:E / WindowManager(2029):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 01-16 12:01:37.393:E / WindowManager(2029):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 01-16 12:01:37.393:E / WindowManager(2029):在android.app.ActivityThread.access $ 600(ActivityThread.java:141) 01-16 12:01:37.393:E / WindowManager(2029):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256) 01-16 12:01:37.393:E / WindowManager(2029):在android.os.Handler.dispatchMessage(Handler.java:99) 01-16 12:01:37.393:E / WindowManager(2029):在android.os.Looper.loop(Looper.java:137) 01-16 12:01:37.393:E / WindowManager(2029):在android.app.ActivityThread.main(ActivityThread.java:5103) 01-16 12:01:37.393:E / WindowManager(2029):at java.lang.reflect.Method.invokeNative(Native Method) 01-16 12:01:37.393:E / WindowManager(2029):at java.lang.reflect.Method.invoke(Method.java:525) 01-16 12:01:37.393:E / WindowManager(2029):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 01-16 12:01:37.393:E / WindowManager(2029):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 01-16 12:01:37.393:E / WindowManager(2029):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
你看过错误了!!! 引起:java.net.ConnectException:无法连接到/192.168.1.4(端口80):连接失败:
可能你的mysql连接无法正常工作。您可以先发出ping请求来检查服务器是否找到,但是您可以确定您的mysql连接属性。
答案 1 :(得分:0)
无论您在何处实例化AsyncTask&#34; CreateNewProduct&#34;,(我都没有看到此代码已发布),它应该看起来像这样:
CreateNewProduct createNewProduct = new CreateNewProduct(productString1, productString2... etc);
createNewProduct.execute();
然后它会实际运行。如果您打算使用它们,请传入productString变量。
如果没有看到相关代码的其余部分(CreateNewProduct实例化,以及您编写的其余AsyncTask代码以及其他任何相关内容),我不确定还有什么可以帮助 - 但如果您发布更多内容,我一定会尝试提供进一步的帮助! :)