我的AsyncTask中有一些错误

时间:2014-06-11 05:51:10

标签: java android eclipse android-asynctask

我传递的数据会插入到我的MySQL数据库中,但在完成它的工作后,我的应用程序强制关闭,我真的无法弄明白,为什么!这是我的代码和LogCat

06-11 11:17:00.148: I/ApplicationPackageManager(29392): cscCountry is not German : INU
06-11 11:17:02.460: D/dalvikvm(29392): GC_EXTERNAL_ALLOC freed 182K, 47% free 2971K/5575K, external 962K/1036K, paused 42ms
06-11 11:17:15.531: D/dalvikvm(29392): GC_CONCURRENT freed 109K, 44% free 3272K/5831K, external 1129K/1615K, paused 4ms+4ms
06-11 11:17:15.546: E/JSON Parser(29392): Error parsing data org.json.JSONException: Value success of type java.lang.String cannot be converted to JSONObject
06-11 11:17:15.546: W/dalvikvm(29392): threadid=9: thread exiting with uncaught exception (group=0x40018578)
06-11 11:17:15.554: E/AndroidRuntime(29392): FATAL EXCEPTION: AsyncTask #1
06-11 11:17:15.554: E/AndroidRuntime(29392): java.lang.RuntimeException: An error occured while executing doInBackground()
06-11 11:17:15.554: E/AndroidRuntime(29392):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-11 11:17:15.554: E/AndroidRuntime(29392):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
06-11 11:17:15.554: E/AndroidRuntime(29392):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
06-11 11:17:15.554: E/AndroidRuntime(29392):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
06-11 11:17:15.554: E/AndroidRuntime(29392):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-11 11:17:15.554: E/AndroidRuntime(29392):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-11 11:17:15.554: E/AndroidRuntime(29392):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-11 11:17:15.554: E/AndroidRuntime(29392):    at java.lang.Thread.run(Thread.java:1019)
06-11 11:17:15.554: E/AndroidRuntime(29392): Caused by: java.lang.NullPointerException
06-11 11:17:15.554: E/AndroidRuntime(29392):    at com.vesit.fragments.Registration$CreateNewProduct.doInBackground(Registration.java:81)
06-11 11:17:15.554: E/AndroidRuntime(29392):    at com.vesit.fragments.Registration$CreateNewProduct.doInBackground(Registration.java:1)
06-11 11:17:15.554: E/AndroidRuntime(29392):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-11 11:17:15.554: E/AndroidRuntime(29392):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
06-11 11:17:15.554: E/AndroidRuntime(29392):    ... 4 more
06-11 11:17:22.234: E/WindowManager(29392): Activity com.vesit.praxis.NavigationMain has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40558578 that was originally added here
06-11 11:17:22.234: E/WindowManager(29392): android.view.WindowLeaked: Activity com.vesit.praxis.NavigationMain has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40558578 that was originally added here
06-11 11:17:22.234: E/WindowManager(29392):     at android.view.ViewRoot.<init>(ViewRoot.java:263)
06-11 11:17:22.234: E/WindowManager(29392):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:171)
06-11 11:17:22.234: E/WindowManager(29392):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
06-11 11:17:22.234: E/WindowManager(29392):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
06-11 11:17:22.234: E/WindowManager(29392):     at android.app.Dialog.show(Dialog.java:241)
06-11 11:17:22.234: E/WindowManager(29392):     at com.vesit.fragments.Registration$CreateNewProduct.onPreExecute(Registration.java:64)
06-11 11:17:22.234: E/WindowManager(29392):     at android.os.AsyncTask.execute(AsyncTask.java:391)
06-11 11:17:22.234: E/WindowManager(29392):     at com.vesit.fragments.Registration$1.onClick(Registration.java:44)
06-11 11:17:22.234: E/WindowManager(29392):     at android.view.View.performClick(View.java:2485)
06-11 11:17:22.234: E/WindowManager(29392):     at android.view.View$PerformClick.run(View.java:9080)
06-11 11:17:22.234: E/WindowManager(29392):     at android.os.Handler.handleCallback(Handler.java:587)
06-11 11:17:22.234: E/WindowManager(29392):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 11:17:22.234: E/WindowManager(29392):     at android.os.Looper.loop(Looper.java:130)
06-11 11:17:22.234: E/WindowManager(29392):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-11 11:17:22.234: E/WindowManager(29392):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 11:17:22.234: E/WindowManager(29392):     at java.lang.reflect.Method.invoke(Method.java:507)
06-11 11:17:22.234: E/WindowManager(29392):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-11 11:17:22.234: E/WindowManager(29392):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-11 11:17:22.234: E/WindowManager(29392):     at dalvik.system.NativeStart.main(Native Method)

这是代码

public class Registration extends Fragment  {
private ProgressDialog pDialog; 
JSONParser jsonParser = new JSONParser();
EditText inputFname;
EditText inputLname;
EditText inputEmail;
EditText inputEventname;
EditText inputContact;
private static String url_create_product = "http://rideconnect.url.ph/registration.php";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.registration, container, false);
    rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));   
    inputFname = (EditText) rootView. findViewById(R.id.inputFname);
    inputLname = (EditText)  rootView.findViewById(R.id.inputLname);
    inputEmail = (EditText) rootView. findViewById(R.id.inputEmail);
    inputContact = (EditText) rootView. findViewById(R.id.inputContact);
    inputEventname = (EditText) rootView.findViewById(R.id.inputEventname);
    Button btnCreateProduct = (Button) rootView. findViewById(R.id.btnCreateProduct);
    btnCreateProduct.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new CreateNewProduct().execute();
            }
        });
    return rootView;    
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true);
}

class CreateNewProduct extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Generating Coupon. Please Wait... ");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground(String... args) {
        String fname = inputFname.getText().toString();
        String lname = inputLname.getText().toString();
        String contact = inputContact.getText().toString();
        String email = inputEmail.getText().toString();
        String eventname = inputEventname.getText().toString();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("fname", fname));
        params.add(new BasicNameValuePair("lname", lname));
        params.add(new BasicNameValuePair("contact", contact));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("eventname", eventname));
        JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                "POST", params);
        Log.d("Create Response", json.toString());
        return null;
    }

    protected void onPostExecute() {
        pDialog.dismiss();
    }
}
}

3 个答案:

答案 0 :(得分:1)

您的Link会将输出显示为简单字符串,如下所示:

success

您需要JSON作为输出:

{"result":"success"}

使用json_encode()

在PHP端将其转换为JSON

示例: json_encode(array("result" => "success"));

答案 1 :(得分:0)

有时候,当您从服务器获取数据时,当您编写String时会添加一些不需要的字符。所以试试这会对你有帮助。

而不是仅返回json对象从

返回
return yourjSonObj;

return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1)

答案 2 :(得分:0)

JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params);

您正在尝试将数据直接转换为JSON Object ..我认为您从服务器获取的数据不是JSON Obect类型..可以是Json Arrray ..如果您尝试将JSOn数组转换为json对象它通过例外.. 你应该测试来自服务器的数据是JSon对象的类型.. ??