URLに接続し、ユーザー名とパスワードをURLに送信すると、URLの応答が返されます。このドキュメントを読み、GET
メソッドを使用します。しかし、プログラムを実行すると、プログラムが停止しました。なんで?
GetFromURL.java:
@SuppressLint("NewApi")
class GetFromURL extends AsyncTask<String,Void, String> {
InputStream is = null;
@Override
protected String doInBackground(String... urls) {
URL url=null;
String response=null;
String parameters=null;
OutputStreamWriter request=null;
try {
url = new URL("http://www.authentication.com/login.php");//This is not real URL.
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setDoOutput(true);
//setRequestProperty();
urlConnection.setRequestMethod("GET");
request=new OutputStreamWriter(urlConnection.getOutputStream());
request.write(parameters);
InputStreamReader inStream=new InputStreamReader(urlConnection.getInputStream());
response=convertStreamToString(inStream);
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
private static String convertStreamToString(InputStreamReader is) {
BufferedReader reader = new BufferedReader(is);
StringBuilder sb = new StringBuilder();
String line ="";
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
MainActivity.java:
public class MainActivity extends Activity {
String user=null;
String pass=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText user_id;
EditText pass_id;
Button button;
user_id=(EditText)findViewById(R.id.user_id);
pass_id=(EditText)findViewById(R.id.pass);
button=(Button)findViewById(R.id.button_click);
user=user_id.getText().toString();
pass=pass_id.getText().toString();
button.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener=new OnClickListener(){
public void onClick(View v){
AsyncTask<String, Void, String> async;
String result="test";
async=new GetFromURL().execute("U="+user+"&K="+pass);
try {
result = async.get();
Log.i("RESULT", result);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
例外:
11-28 14:55:48.757: I/catch2(19663): of main
11-28 14:55:48.757: W/System.err(19663): java.util.concurrent.ExecutionException: java.lang.NullPointerException
11-28 14:55:48.757: W/System.err(19663): at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:223)
11-28 14:55:48.765: W/System.err(19663): at java.util.concurrent.FutureTask.get(FutureTask.java:82)
11-28 14:55:48.765: W/System.err(19663): at android.os.AsyncTask.get(AsyncTask.java:462)
11-28 14:55:48.765: W/System.err(19663): at org.example.reward.MainActivity$1.onClick(MainActivity.java:75)
11-28 14:55:48.765: W/System.err(19663): at android.view.View.performClick(View.java:3480)
11-28 14:55:48.765: W/dalvikvm(19663): threadid=11: thread exiting with uncaught exception (group=0x40a4a1f8)
11-28 14:55:48.765: W/System.err(19663): at android.view.View$PerformClick.run(View.java:13983)
11-28 14:55:48.765: W/System.err(19663): at android.os.Handler.handleCallback(Handler.java:605)
11-28 14:55:48.765: W/System.err(19663): at android.os.Handler.dispatchMessage(Handler.java:92)
11-28 14:55:48.765: W/System.err(19663): at android.os.Looper.loop(Looper.java:137)
11-28 14:55:48.765: W/System.err(19663): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-28 14:55:48.765: W/System.err(19663): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 14:55:48.765: W/System.err(19663): at java.lang.reflect.Method.invoke(Method.java:511)
11-28 14:55:48.772: W/System.err(19663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-28 14:55:48.772: W/System.err(19663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-28 14:55:48.772: W/System.err(19663): at dalvik.system.NativeStart.main(Native Method)
11-28 14:55:48.772: W/System.err(19663): Caused by: java.lang.NullPointerException
11-28 14:55:48.772: W/System.err(19663): at java.io.Writer.write(Writer.java:141)
11-28 14:55:48.772: W/System.err(19663): at org.example.reward.GetFromURL.doInBackground(GetFromURL.java:59)
11-28 14:55:48.772: W/System.err(19663): at org.example.reward.GetFromURL.doInBackground(GetFromURL.java:1)
11-28 14:55:48.772: W/System.err(19663): at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-28 14:55:48.772: W/System.err(19663): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-28 14:55:48.772: W/System.err(19663): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-28 14:55:48.772: W/System.err(19663): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-28 14:55:48.772: W/System.err(19663): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-28 14:55:48.772: W/System.err(19663): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-28 14:55:48.772: W/System.err(19663): at java.lang.Thread.run(Thread.java:856)
11-28 14:55:48.772: E/AndroidRuntime(19663): FATAL EXCEPTION: AsyncTask #1
11-28 14:55:48.772: E/AndroidRuntime(19663): java.lang.RuntimeException: An error occured while executing doInBackground()
11-28 14:55:48.772: E/AndroidRuntime(19663): at android.os.AsyncTask$3.done(AsyncTask.java:278)
11-28 14:55:48.772: E/AndroidRuntime(19663): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-28 14:55:48.772: E/AndroidRuntime(19663): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-28 14:55:48.772: E/AndroidRuntime(19663): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-28 14:55:48.772: E/AndroidRuntime(19663): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-28 14:55:48.772: E/AndroidRuntime(19663): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-28 14:55:48.772: E/AndroidRuntime(19663): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-28 14:55:48.772: E/AndroidRuntime(19663): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-28 14:55:48.772: E/AndroidRuntime(19663): at java.lang.Thread.run(Thread.java:856)
11-28 14:55:48.772: E/AndroidRuntime(19663): Caused by: java.lang.NullPointerException
11-28 14:55:48.772: E/AndroidRuntime(19663): at java.io.Writer.write(Writer.java:141)
11-28 14:55:48.772: E/AndroidRuntime(19663): at org.example.reward.GetFromURL.doInBackground(GetFromURL.java:59)
11-28 14:55:48.772: E/AndroidRuntime(19663): at org.example.reward.GetFromURL.doInBackground(GetFromURL.java:1)
11-28 14:55:48.772: E/AndroidRuntime(19663): at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-28 14:55:48.772: E/AndroidRuntime(19663): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-28 14:55:48.772: E/AndroidRuntime(19663): ... 5 more
11-28 14:55:49.265: D/OpenGLRenderer(19663): Flushing caches (mode 0)
11-28 14:55:49.796: D/OpenGLRenderer(19663): Flushing caches (mode 1)
11-28 14:55:50.140: I/Process(19663): Sending signal. PID: 19663 SIG: 9
11-28 15:01:52.538: D/libEGL(19852): loaded /system/lib/egl/libGLES_android.so
11-28 15:01:52.538: D/libEGL(19852): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
11-28 15:01:52.554: D/libEGL(19852): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
11-28 15:01:52.561: D/libEGL(19852): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
11-28 15:01:52.726: D/OpenGLRenderer(19852): Enabling debug mode 0
11-28 15:02:08.827: D/dalvikvm(19852): GC_CONCURRENT freed 157K, 2% free 14304K/14535K, paused 3ms+1ms
11-28 15:02:09.991: I/catch2(19852): of main
11-28 15:02:09.991: W/System.err(19852): java.util.concurrent.ExecutionException: java.lang.NullPointerException
11-28 15:02:09.999: W/System.err(19852): at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:223)
11-28 15:02:09.999: W/System.err(19852): at java.util.concurrent.FutureTask.get(FutureTask.java:82)
11-28 15:02:09.999: W/System.err(19852): at android.os.AsyncTask.get(AsyncTask.java:462)
11-28 15:02:09.999: W/System.err(19852): at org.example.reward.MainActivity$1.onClick(MainActivity.java:75)
11-28 15:02:09.999: W/System.err(19852): at android.view.View.performClick(View.java:3480)
11-28 15:02:09.999: W/System.err(19852): at android.view.View$PerformClick.run(View.java:13983)
11-28 15:02:09.999: W/System.err(19852): at android.os.Handler.handleCallback(Handler.java:605)
11-28 15:02:09.999: W/System.err(19852): at android.os.Handler.dispatchMessage(Handler.java:92)
11-28 15:02:09.999: W/System.err(19852): at android.os.Looper.loop(Looper.java:137)
11-28 15:02:09.999: W/System.err(19852): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-28 15:02:09.999: W/System.err(19852): at java .lang.reflect.Method.invokeNative(Native Method)
11-28 15:02:09.999: W/System.err(19852): at java.lang.reflect.Method.invoke(Method.java:511)
11-28 15:02:09.999: W/System.err(19852): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-28 15:02:09.999: W/System.err(19852): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-28 15:02:09.999: W/System.err(19852): at dalvik.system.NativeStart.main(Native Method)
11-28 15:02:09.999: W/System.err(19852): Caused by: java.lang.NullPointerException
11-28 15:02:09.999: W/System.err(19852): at java.io.Writer.write(Writer.java:141)
11-28 15:02:09.999: W/System.err(19852): at org.example.reward.GetFromURL.doInBackground(GetFromURL.java:59)
11-28 15:02:09.999: W/System.err(19852): at org.example.reward.GetFromURL.doInBackground(GetFromURL.java:1)
11-28 15:02:09.999: W/System.err(19852): at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-28 15:02:09.999: W/System.err(19852): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-28 15:02:09.999: W/System.err(19852): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-28 15:02:09.999: W/System.err(19852): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-28 15:02:09.999: W/System.err(19852): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-28 15:02:09.999: W/System.err(19852): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-28 15:02:09.999: W/System.err(19852): at java.lang.Thread.run(Thread.java:856)
11-28 15:02:10.007: W/dalvikvm(19852): threadid=12: thread exiting with uncaught exception (group=0x40a4a1f8)
11-28 15:02:10.015: E/AndroidRuntime(19852): FATAL EXCEPTION: AsyncTask #1
11-28 15:02:10.015: E/AndroidRuntime(19852): java.lang.RuntimeException: An error occured while executing doInBackground()
11-28 15:02:10.015: E/AndroidRuntime(19852): at android.os.AsyncTask$3.done(AsyncTask.java:278)
11-28 15:02:10.015: E/AndroidRuntime(19852): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-28 15:02:10.015: E/AndroidRuntime(19852): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-28 15:02:10.015: E/AndroidRuntime(19852): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-28 15:02:10.015: E/AndroidRuntime(19852): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-28 15:02:10.015: E/AndroidRuntime(19852): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-28 15:02:10.015: E/AndroidRuntime(19852): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-28 15:02:10.015: E/AndroidRuntime(19852): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-28 15:02:10.015: E/AndroidRuntime(19852): at java.lang.Thread.run(Thread.java:856)
11-28 15:02:10.015: E/AndroidRuntime(19852): Caused by: java.lang.NullPointerException
11-28 15:02:10.015: E/AndroidRuntime(19852): at java.io.Writer.write(Writer.java:141)
11-28 15:02:10.015: E/AndroidRuntime(19852): at org.example.reward.GetFromURL.doInBackground(GetFromURL.java:59)
11-28 15:02:10.015: E/AndroidRuntime(19852): at org.example.reward.GetFromURL.doInBackground(GetFromURL.java:1)
11-28 15:02:10.015: E/AndroidRuntime(19852): at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-28 15:02:10.015: E/AndroidRuntime(19852): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-28 15:02:10.015: E/AndroidRuntime(19852): ... 5 more
11-28 15:02:10.249: D/OpenGLRenderer(19852): Flushing caches (mode 0)
11-28 15:02:10.788: D/OpenGLRenderer(19852): Flushing caches (mode 1)
11-28 15:02:12.163: I/Process(19852): Sending signal. PID: 19852 SIG: 9
手伝ってくれてありがとう。