Android应用未连接到xamp数据库

时间:2016-02-02 06:51:48

标签: android listview arraylist android-adapter

它没有错误,但是当运行和启动活动时它给app_name停止工作。在android清单中我添加了互联网权限.URL路径工作得非常好,因为我直接将它放在浏览器中它给出了所需的结果。LogCat我是Android应用程序开发的新手。请帮助我,我需要它用于最后一年的项目。 StartingPoint.java

public class StartingPoint extends Activity implements OnClickListener {
Button  bed;

@Override
protected void onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.activity_starting_point);

    inialize();



    bed.setOnClickListener(MenuListener);


}

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

    bed = (Button) findViewById(R.id.bActivityBed);
    mirror = (Button) findViewById(R.id.bActivityMirror);
}


private OnClickListener MenuListener = new OnClickListener()  {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
        case R.id.bActivitySofa:
            Intent i = new Intent("com.example.pb.SOFA");
            startActivity(i);
            break;

        case R.id.bActivityChair:
            Intent chair = new Intent("com.example.pb.CHAIR");
            startActivity(chair);
            break;
        case R.id.bActivityBed:
            Intent bed = new Intent("com.example.pb.BED");
            startActivity(bed);
            break;
        case R.id.bActivityMirror:
            Intent mirror = new Intent("com.example.pb.MIRROR");
            startActivity(mirror);
            break;
        }
    }

};}

Bed.java((可能是导致问题的实际类))

public class Bed extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;

private static String url_all_products="http://192.168.1.3/AndriodConnect/ReadProducts.php";

private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "prodId";
private static final String TAG_NAME = "prodName";

JSONArray products = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bed);

    productsList = new ArrayList<HashMap<String,String>>();

    new LoadAllProducts().execute();

    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String pid = ((TextView) view.findViewById(R.id.tvId)).getText()
                    .toString();

            Intent in = new Intent(getApplicationContext(),
                    Sofa.class);
            in.putExtra(TAG_PID, pid);
            startActivityForResult(in, 100);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==100) {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }
}
public class LoadAllProducts extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pDialog = new ProgressDialog(Bed.this);
        pDialog.setMessage("Loading products.Plz wait");
        pDialog.setIndeterminate(false);
        pDialog.show();
    }
    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(url_all_products, "Get", params);
        Log.d("All products", json.toString());
        try {
            int success = json.getInt(TAG_SUCCESS);
            if(success == 1) {
                products = json.getJSONArray(TAG_PRODUCTS);
                for(int i=0; i<products.length();i++) {
                    JSONObject c = products.getJSONObject(i);
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);
                    HashMap<String,String> map = new HashMap<String,String>();
                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);
                    productsList.add(map);
                }
            }
            }
            catch(JSONException e) {
                e.printStackTrace();
            }

        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
        public void run() { 

        ListAdapter adapter = new SimpleAdapter(Bed.this, productsList,
                R.layout.list_item,new String[] {TAG_PID,TAG_NAME},
                new int[] {R.id.tvId, R.id.tvName});
        setListAdapter(adapter);
        }
        });
    }

}

}

JSONParser.java

public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
        if(method == "POST"){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

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

        }else if(method == "GET"){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

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


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException 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);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".StartingPoint"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.pb.STARTINGPOINT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Login"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.pb.LOGIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Prefs"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.pb.PREFS" />
           <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".AboutUs"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.pb.ABOUTUS" />
           <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Sofa"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.pb.SOFA" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
           <activity
        android:name=".Mirror"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.pb.MIRROR" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
           <activity
        android:name=".Bed"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.pb.BED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
           <activity
        android:name=".Chair"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.pb.CHAIR" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
          <activity
        android:name=".AllProductsActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.pb.PRODUCTS" /> 
           <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
   </application>
</manifest>

1 个答案:

答案 0 :(得分:0)

我使用WAMP,因为必须在通知中检查“PUT Online”,否则它不会连接。您应该在xamp中找到相同的内容。我只是因为这个原因而改用WAMP。

以及Android和您的系统应该在同一个网络(WAN或LAN)中。