2016-02-02 71 views
0

它給出了沒有錯誤,但是當一個運行和開始活動它給app_name停止工作。在Android清單我添加了互聯網permission.URL路徑工作完全正常,因爲我把它放在瀏覽器中直接它給予所需結果。 LogCat。我是Android應用程序開發新手。很好的幫助我,我需要它的最後一年的項目。 StartingPoint.javaAndroid應用程序沒有連接到xamp數據庫

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> 
+0

你得到的錯誤是什麼? – Pavya

+0

,並在清單 – Pavya

+0

中添加並在清單中添加 Pavya

回答

0

一世使用WAMP中的「PUT Online」必須在通知中檢查,否則它不會連接。你應該在xamp中找到同樣的東西。因爲這個原因,我切換到了WAMP。

還有Android和你的系統應該在同一個網絡(WAN或LAN)。

+0

Xamp服務器已啓動。 – Infinity

相關問題