2011-11-24 68 views
0

我的Java代码:的Android连接到MySQL(PHP脚本)

package com.Cmode.ThesisSystem; 

    import java.io.BufferedReader; 
    import java.io.InputStream; 
    import java.io.InputStreamReader; 
    import java.util.ArrayList; 
    import org.apache.http.HttpEntity; 
    import org.apache.http.HttpResponse; 
    import org.apache.http.NameValuePair; 
    import org.apache.http.client.HttpClient; 
    import org.apache.http.client.entity.UrlEncodedFormEntity; 
    import org.apache.http.client.methods.HttpPost; 
    import org.apache.http.impl.client.DefaultHttpClient; 
    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 
    import android.app.ListActivity; 
    import android.net.ParseException; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.widget.Toast; 


public class EventLogs extends ListActivity { 

JSONArray jArray; 
String result = null; 
InputStream is = null; 
StringBuilder sb=null; 
int e_id; 
String e_name; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 


ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();{ 


//http post 
try{ 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.my.com/eventlogs.php"); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
is = entity.getContent(); 
}catch(Exception e){ 
    Log.e("log_tag", "Error in http connection"+e.toString()); 

}

//convert response to string 
try{ 
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
sb = new StringBuilder(); 
sb.append(reader.readLine() + "\n"); 
String line="0"; 
while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
    } 
    is.close(); 
    result=sb.toString(); 
    }catch(Exception e){ 
     Log.e("log_tag", "Error converting result "+e.toString()); 
    } 

//配对数据

try{ 
    jArray = new JSONArray(result); 
    JSONObject json_data=null; 
    for(int i=0;i<jArray.length();i++){ 
     json_data = jArray.getJSONObject(i); 
     e_id=json_data.getInt("id"); 
     e_name=json_data.getString("event"); 
    } 
    }catch(JSONException e1){ 
     Toast.makeText(getBaseContext(), "No events found" ,Toast.LENGTH_LONG).show(); 
    } catch (ParseException e1) { 
     e1.printStackTrace(); 
    } 

} 
} 

} 

我有这个代码 错误的问题: 应用程序CDroidMonitoring(process.com.Cmode.ThesisSystem)意外停止。请再试一次

我只是新的android开发。我想我已经把一些不可用的大括号或者代码中出现了错误。

+0

此外,当应用程序说它“意外停止”并退出,并发生异常。看到'logcat'并在这里发布相关错误。 –

+0

@PeterKnego 1-24 17:21:09.491:E/AndroidRuntime(713):未捕获的处理程序:由于未捕获的异常导致主线程退出 – ching

+0

是的,很多helpa。生成它的哪个异常和代码行? –

回答

0

您的应用程序是否在清单中设置了INTERNET权限? 有关错误的更多信息,请查看Eclipse中的logcat选项卡,它通常具有更多描述性错误消息。

+0

有这么多的错误..我也把互联网权限放在清单中..仍然错误是一样的 – ching

+0

您是否检查清单,如果您有正确的权限?同时尝试找到发生的第一个异常,它将有错误的正确原因。 –

+0

11-24 17:31:08.876:W/dalvikvm(715):threadid = 3:线程退出时未捕获的异常(group = 0x4000fe70)和11-24 17:31:08.876:E/AndroidRuntime(715) :线程主要由于未捕获的异常而退出 – ching