我正在尝试使用xampp服务器从mysql数据库中获取数据但我的应用程序在我打开时崩溃了。帮助我

时间:2015-04-02 11:38:26

标签: php android mysql

我是android的新手,我正在尝试使用xampp服务器从mysql数据库中获取名称并在spinner中显示它,但应用程序 主要活动代码:     package com.example.bhargav.insertdata;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {
    Spinner spin;
    InputStream is = null;
    String ip = "http://127.0.0.1//pro/select.php";
    String line = null;
    String result = null;
    List<String> list;

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

        spin = (Spinner) findViewById(R.id.spinner1);

        webserviceCall(); // call webservice activity and get list value

        ArrayAdapter<String> adp = new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, list); // set list        into ArrayAdapter

        spin.setAdapter(adp); // Set Adapter value into Spinner
    }

    private void webserviceCall() {
        // TODO Auto-generated method stub

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(ip);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }
        catch (Exception e) {
            Log.e("Webservice 1", e.toString());
        }
        try {enter code here

            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();
        }
        catch (Exception e) {
            Log.e("Webservice 2", e.toString());
        }
        try {

            JSONArray ja = new JSONArray(result);
            JSONObject jo = null;

            list = new ArrayList<String>();

            for(int i=0; i<ja.length(); i++) {

                jo = ja.getJSONObject(i);
                list.add(jo.getString("uname"));
            }
        }catch (Exception e) {
            Log.e("Webservice 3", e.toString());
        }
    }
}

我也得到空指针exception.plz帮助我。

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends Activity {
    Spinner spin;
    InputStream is = null;
    String ip = "http://127.0.0.1//pro/select.php";
    String line = null;
    String result = null;
    List<String> list;*******************************

您必须在使用前初始化对象。

List<String> list=new ArrayList<String>();