简体   繁体   English

Android连接到MySql(PHP脚本)

[英]Android connecting to MySql (PHP Script)

My java code: 我的Java代码:

  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());
  }

//paring data //准备数据

  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();
  }

}
 }

}

I have a problem with this code Error: The application CDroidMonitoring(process.com.Cmode.ThesisSystem) has stopped unexpectedly. 我对此代码有问题错误:应用程序CDroidMonitoring(process.com.Cmode.ThesisSystem)意外停止。 Please try again 请再试一次

Im just new to android development. 我是android开发的新手。 I think I have put something unusable braces or there is something wrong in the code. 我想我在括号中放了一些无​​法使用的东西,或者在代码中出现了错误。

Does your application have the INTERNET permission set in the manifest? 您的应用程序是否在清单中设置了INTERNET权限? And for more information about the error check the logcat tab in Eclipse, it usually has a more descriptive error message. 有关错误的更多信息,请检查Eclipse中的logcat选项卡,它通常具有更具描述性的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM