从Android应用程序连接到mySql数据库时,应用程序崩溃

时间:2015-12-04 05:08:36

标签: android mysql apache

我正在尝试从我的Android应用程序连接到localhost apache服务器。我正在从博客(AndroidHive)复制代码,因为我之前从未使用过任何这些代码。当我尝试连接到数据库时,我收到以下错误:

12-04 11:00:14.457 7039-7101/com.example.monkey.mysql_dump E/Buffer Error: Error converting result java.lang.NullPointerException: lock == null
12-04 11:00:14.457 7039-7101/com.example.monkey.mysql_dump E/JSON Parser: Error parsing data org.json.JSONException: End of input at character 0 of 
12-04 11:00:14.457 7039-7101/com.example.monkey.mysql_dump W/dalvikvm: threadid=11: thread exiting with uncaught exception (group=0x41621c08)
12-04 11:00:14.467 7039-7101/com.example.monkey.mysql_dump E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1

我的代码如下:

public class All_products extends AppCompatActivity{
static InputStream is = null;
static String json = "";
static JSONObject jObj = null;
static List<NameValuePair> parameter = new ArrayList<NameValuePair>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_products);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    parameter.add(new BasicNameValuePair("cName", "Ibtehaz SHawon"));
    parameter.add(new BasicNameValuePair("cID", "1004200"));
    parameter.add(new BasicNameValuePair("cDue", "400"));
    parameter.add(new BasicNameValuePair("cAmount", "1400"));


    new doInb().execute();
}

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


    @Override
    protected String doInBackground(String... params) {
        String insertLink = "http://localhost/BRACathon_ServerSide/db_insert.php";
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(insertLink);
            httpPost.setEntity(new UrlEncodedFormEntity(parameter));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException 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);
            Toast.makeText(getApplicationContext(), jObj.toString(), Toast.LENGTH_LONG).show();

        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        return jObj.toString();
    }
}

我的Apache服务器配置是

cName == varChar
cID == int
cDue == int
cAmount == int

谢谢。

1 个答案:

答案 0 :(得分:0)

我认为您的网址正在发送Null Pointer Exception。使用手动数据检查db_insert.php文件,并确保在app"module file中包含jar文件。现在DefaultHttpClient已弃用。请使用HttpURLConnection连接数据库。