Android で MySQL にアクセスするために JSON 経由で PHP ファイルに接続しようとすると問題が発生します。これが原因でアプリがクラッシュする理由を一生理解できません。何か案は?
次のエラーがスローされます。
AndroidRuntime
FATAL EXCEPTION: main
私は私の主な活動からこれを呼び出そうとしています:
new getData().getMovie();
これを呼び出す:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.*;
import org.json.*;
import android.util.Log;
import android.widget.Toast;
public class getData extends FullscreenActivity{
public void getMovie() {
String result = "";
String rID="1";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("ids",rID));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://website.com/myphp.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
}catch(Exception e){
Toast.makeText(this, "Error in http connection "+e.toString(), Toast.LENGTH_LONG).show();
//Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
InputStream is = null;
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();
result=sb.toString();
}catch(Exception e){
Toast.makeText(this, "Error converting result "+e.toString(), Toast.LENGTH_LONG).show();
//Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getString("id")+
", 1: "+json_data.getString("1")+
", 2: "+json_data.getString("2")+
", 3: "+json_data.getString("3")+
", 4: "+json_data.getString("4")+
", 5: "+json_data.getString("5")
);
}
} catch (JSONException e) {
Toast.makeText(this, "Error parsing data "+e.toString(), Toast.LENGTH_LONG).show();
//Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}