PHPファイルにHTTPリクエストを送信してから文字列を返すAndroidアプリケーションを開発しています。このコードは Android 2.x 以下では正常に動作しますが、それよりも高い Android OS で実行するとクラッシュします。マニフェスト ファイルに必要なすべての権限があります。
これが私のコードです:
getData data;
String result;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = new getData("http://alexanderpadin.uphero.com/copyDataBase.php");
result = data.getResult();
Log.i("Data", result);
}
そして私のgetDataクラス:
package com.application.uprm_map;
import java.io.InputStream;
import java.io.BufferedReader;
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 android.util.Log;
public class getData {
private String result;
private InputStream is;
private StringBuilder sb;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
public getData(String url){
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
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());
}
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());
}
}
public String getResult(){
return result;
}
}
LogCat:
04-17 01:29:43.593: E/Trace(13235): error opening trace file: No such file or directory (2)
04-17 01:29:44.118: E/log_tag(13235): Error in http connection android.os.NetworkOnMainThreadException
04-17 01:29:44.118: E/log_tag(13235): Error converting result java.lang.NullPointerException: lock == null
04-17 01:29:44.133: E/AndroidRuntime(13235): FATAL EXCEPTION: main
04-17 01:29:44.133: E/AndroidRuntime(13235): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.uprm_map/com.application.uprm_map.MainActivity}: java.lang.NullPointerException: println needs a message
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.app.ActivityThread.access$600(ActivityThread.java:153)
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.os.Looper.loop(Looper.java:137)
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.app.ActivityThread.main(ActivityThread.java:5226)
04-17 01:29:44.133: E/AndroidRuntime(13235): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 01:29:44.133: E/AndroidRuntime(13235): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 01:29:44.133: E/AndroidRuntime(13235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-17 01:29:44.133: E/AndroidRuntime(13235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-17 01:29:44.133: E/AndroidRuntime(13235): at dalvik.system.NativeStart.main(Native Method)
04-17 01:29:44.133: E/AndroidRuntime(13235): Caused by: java.lang.NullPointerException: println needs a message
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.util.Log.println_native(Native Method)
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.util.Log.i(Log.java:159)
04-17 01:29:44.133: E/AndroidRuntime(13235): at com.application.uprm_map.MainActivity.onCreate(MainActivity.java:64)
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.app.Activity.performCreate(Activity.java:5104)
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-17 01:29:44.133: E/AndroidRuntime(13235): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
04-17 01:29:44.133: E/AndroidRuntime(13235): ... 11 more
また、このパッケージを使用してみました: http://loopj.com/android-async-http/ :
コードは次のとおりです。
String result;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://alexanderpadin.uphero.com/copyDataBase", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
result = response;
}
});
log.i("Data", result);
}
関数 onSuccess 内で応答変数を出力しようとすると機能しますが、その変数を取得して何か他のことを実行しようとすると機能しません。
LogCat:
04-17 01:22:11.358: E/Trace(12475): error opening trace file: No such file or directory (2)
04-17 01:22:26.103: E/Trace(12710): error opening trace file: No such file or directory (2)
04-17 01:22:26.618: E/AndroidRuntime(12710): FATAL EXCEPTION: main
04-17 01:22:26.618: E/AndroidRuntime(12710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.uprm_map/com.application.uprm_map.MainActivity}: java.lang.NullPointerException: println needs a message
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.app.ActivityThread.access$600(ActivityThread.java:153)
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.os.Looper.loop(Looper.java:137)
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.app.ActivityThread.main(ActivityThread.java:5226)
04-17 01:22:26.618: E/AndroidRuntime(12710): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 01:22:26.618: E/AndroidRuntime(12710): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 01:22:26.618: E/AndroidRuntime(12710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-17 01:22:26.618: E/AndroidRuntime(12710): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-17 01:22:26.618: E/AndroidRuntime(12710): at dalvik.system.NativeStart.main(Native Method)
04-17 01:22:26.618: E/AndroidRuntime(12710): Caused by: java.lang.NullPointerException: println needs a message
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.util.Log.println_native(Native Method)
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.util.Log.i(Log.java:159)
04-17 01:22:26.618: E/AndroidRuntime(12710): at com.application.uprm_map.MainActivity.onCreate(MainActivity.java:69)
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.app.Activity.performCreate(Activity.java:5104)
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-17 01:22:26.618: E/AndroidRuntime(12710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
04-17 01:22:26.618: E/AndroidRuntime(12710): ... 11 more
私のコードに欠けているものはありますか? 誰かがこれを達成するのを手伝ってくれますか? どんな助けでも感謝します。ありがとうございました。