在android中创建一个http请求它会停止并且应用程序强制关闭

时间:2013-10-15 14:20:53

标签: php android mysql json xmlhttprequest

我试图在Android上用JSON和php / mysql上的HTTP请求来显示mysql数据库中的一些数据。

应用程序只需按一个按钮,它将在日志cat中显示数据库中的选定数据,并使用intent在模拟器上显示第二页。

问题是,当我尝试运行应用程序时,系统会在将请求发送到http之前进行压缩,并且我使用localhost服务器(wamp),因此我使用的URL是:http:// 10.0。 2.2 / studentservice / StudentService / getStudentByID.php

按下按钮后,这是

的logcat

10-15 14:07:28.160: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:28.320: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:28.630: D/gralloc_goldfish(854): Emulator without GPU emulation detected.
10-15 14:07:28.660: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:28.670: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:35.800: E/Error befor http(854): Error before the http request
10-15 14:07:36.210: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:36.230: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:36.730: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:36.750: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:37.250: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:37.270: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:37.770: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:37.780: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:38.281: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:38.300: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:38.800: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:38.821: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:48.400: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:48.460: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:10:25.630: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:10:25.650: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
粉碎log cat display 后

10-15 14:35:13.781: E/AndroidRuntime(906): FATAL EXCEPTION: main
10-15 14:35:13.781: E/AndroidRuntime(906): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lebdev.fitguide.activities/com.lebdev.fitguide.activities.SecondaryActivity}: java.lang.NullPointerException: println needs a message
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.os.Looper.loop(Looper.java:137)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread.main(ActivityThread.java:4424)
10-15 14:35:13.781: E/AndroidRuntime(906):  at java.lang.reflect.Method.invokeNative(Native Method)
10-15 14:35:13.781: E/AndroidRuntime(906):  at java.lang.reflect.Method.invoke(Method.java:511)
10-15 14:35:13.781: E/AndroidRuntime(906):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-15 14:35:13.781: E/AndroidRuntime(906):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-15 14:35:13.781: E/AndroidRuntime(906):  at dalvik.system.NativeStart.main(Native Method)
10-15 14:35:13.781: E/AndroidRuntime(906): Caused by: java.lang.NullPointerException: println needs a message
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.util.Log.println_native(Native Method)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.util.Log.e(Log.java:231)
10-15 14:35:13.781: E/AndroidRuntime(906):  at com.lebdev.fitguide.controller.HttpManager.getResponseFromURL(HttpManager.java:70)
10-15 14:35:13.781: E/AndroidRuntime(906):  at com.lebdev.fitguide.activities.SecondaryActivity.onCreate(SecondaryActivity.java:54)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.Activity.performCreate(Activity.java:4465)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-15 14:35:13.781: E/AndroidRuntime(906):  ... 11 more
10-15 14:35:14.341: I/dalvikvm(906): threadid=3: reacting to signal 3
10-15 14:35:14.370: I/dalvikvm(906): Wrote stack traces to '/data/anr/traces.txt'

MainActivity

package com.lebdev.fitguide.activities;

import com.lebdev.fitguide.businessModel.Student;
import com.lebdev.fitguide.businessModel.Subject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    EditText txtMessage;
    TextView lblMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtMessage = (EditText) findViewById(R.id.txtMessage);
        lblMessage = (TextView) findViewById(R.id.lblMessage);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void Onclick_btnSend(View v) {

         Intent intent  = new Intent(this, SecondaryActivity.class);
         //intent.putExtra("message", txtMessage.getText().toString());
         Student std = new Student(txtMessage.getText().toString(),1234, 1);
         std.getSubjects().add(new Subject("itc", 5));
         intent.putExtra("Student", std);
         startActivity(intent);

         Toast.makeText(this, "i am sending a message here", Toast.LENGTH_SHORT).show();

    }
}

JsonObjectMapper

package com.lebdev.fitguide.om;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.util.Log;

import com.lebdev.fitguide.businessModel.Student;

public class JsonObjectMapper {

    public static Student jsonToStudent(JSONObject jsonObject) {

        Student std = null;

        try {

            std = new Student(jsonObject.getJSONArray("Student")
                    .getJSONObject(0).getString("Name"),
                    Integer.parseInt(jsonObject.getJSONArray("Student")
                            .getJSONObject(0).getString("index")),
                    Integer.parseInt(jsonObject.getJSONArray("Student")
                            .getJSONObject(0).getString("ID")));
        } catch (Exception ex) {
            Log.e("Error Json Converter", ex.getMessage());
        }

        return std;
    }

    public static List<Student> jsonToStudentList(JSONObject jsonObject) {

        List<Student> stdList = new ArrayList<Student>();
        try {

            for (int i = 0; i < jsonObject.getJSONArray("Student").length(); i++) {

                stdList.add(new Student(jsonObject.getJSONArray("Student")
                        .getJSONObject(i).getString("Name"), Integer
                        .parseInt(jsonObject.getJSONArray("Student")
                                .getJSONObject(i).getString("index")), Integer
                        .parseInt(jsonObject.getJSONArray("Student")
                                .getJSONObject(i).getString("ID"))));
            }
        } catch (Exception ex) {

            Log.e("Error Json Converter", ex.getMessage());
        }

        return stdList;
    }
}

SecondaryObject

package com.lebdev.fitguide.activities;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.lebdev.fitguide.businessModel.Student;
import com.lebdev.fitguide.controller.HttpManager;
import com.lebdev.fitguide.controller.JSONParser;
import com.lebdev.fitguide.om.JsonObjectMapper;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class SecondaryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("sid", "2"));

        Log.e("Error befor http", "Error before the http request");
        JSONObject jsonObj = JSONParser
                .parseJSONFromString(new HttpManager()
                        .getResponseFromURL(
                                "http://10.0.2.2/studentservice/StudentService/getStudentByID.php",
                                params));

        Log.e("Error after http", "Error after the http request");

        Student std = JsonObjectMapper.jsonToStudent(jsonObj);

        Log.e("STUDENT DATA", "ID:" + std.getID() +     "Name:" + std.getName()
                + "Index: " + std.getIndex());
/*
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            TextView lblMessage = (TextView) findViewById(R.id.lblSentMessage);
            Student student = bundle.getParcelable("Student");

            lblMessage.setText("I 've recieved a student with a name "
                    + student.getName() + " and has a Subject "
                    + student.getSubjects().get(0).getName() + " with a grade "
                    + student.getSubjects().get(0).getGrade());
        }
*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.secondary, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

10-15 14:35:13.781: E/AndroidRuntime(906): at com.lebdev.fitguide.controller.HttpManager.getResponseFromURL(HttpManager.java:70)

这是一个指向代码,行号和所有内容的指针,指向空指针(Log.e()的空消息)来自哪里并导致崩溃。

从你的logcat中可以看出这一点,所以你在

中做了些什么

JSONObject jsonObj = JSONParser .parseJSONFromString(new HttpManager() .getResponseFromURL( "http://10.0.2.2/studentservice/StudentService/getStudentByID.php", params));

这导致写入Log.e()的空信息。