Java Android应用程序错误与我的代码
以下代码无法连接到SQLiteJava Android应用程序错误与我的代码,java,android,json,apache,Java,Android,Json,Apache,以下代码无法连接到SQLite public class MainActivity extends ListActivity { JSONArray jArray; String result = null; InputStream is = null; StringBuilder sb=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(sa
public class MainActivity extends ListActivity {
JSONArray jArray; String result = null; InputStream is = null;
StringBuilder sb=null;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>(); //http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx/");
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());
} //paring data
int Ravid_id;
String Ravid_Name;
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
Ravid_id=json_data.getInt("Ravid_id");
Ravid_Name=json_data.getString("Ravid_Name");
}
} catch(JSONException e1){
Toast.makeText(getBaseContext(), "No found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
public类MainActivity扩展了ListActivity{
JSONArray jArray;字符串结果=null;InputStream=null;
StringBuilder sb=null;
@在创建时覆盖公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList nameValuePairs=新建
ArrayList();//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://xxx.xxx.xxx.xxx/");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
sb=新的StringBuilder();
sb.append(reader.readLine()+“n”);
字符串行=“0”;
而((line=reader.readLine())!=null){
sb.附加(第+行“n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}//配对数据
国际性;
字符串Ravid_名称;
试一试{
jArray=新的JSONArray(结果);
JSONObject json_data=null;
对于(inti=0;i这只是一个猜测,与一个经常发布的问题有关:您不应该在主线程上执行任何网络操作
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx/");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
…应放在AsncTask
或服务中。有关如何解决此问题的示例,请参阅(通常是android.os.NetworkOnMainThreadException
)。是否可以发布显示错误的日志输出?