I have this class:
package com.problemio;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginActivity extends Activity
{
private TextView textView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Show form for login_email
final EditText loginEmail = (EditText) findViewById(R.id.login_email);
String name = loginEmail.getText().toString();
// Show field for password
final EditText password = (EditText) findViewById(R.id.password);
String text = password.getText().toString();
// Show button for submit
Button submit = (Button)findViewById(R.id.submit);
// Show options for create-profile and forgot-password
//readWebpage( R.layout.login) ;
submit.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
String email = loginEmail.getText().toString();
String pass = password.getText().toString();
sendFeedback(pass, email);
}
});
}
public void sendFeedback(String pass , String email)
{
Log.d( "1" , pass );
Log.d( "1" , email );
String[] params = new String[] { "myurl", email, pass };
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(params);
Log.d( "2" , "After task.execute call" );
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... theParams)
{
Log.d( "Inner class: " , "Doing stuff in background" );
String myUrl = theParams[0];
String myEmail = theParams[1];
String myPassword = theParams[2];
Log.d( "Inner myURL: " , myUrl );
Log.d( "myEmail: " , myEmail );
Log.d( "myPass: " , myPassword );
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", myEmail ));
postParameters.add(new BasicNameValuePair("password", myPassword ));
String response = "";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(myUrl);
try
{
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null)
{
response += s;
}
Log.d( "After call, response: " , " " + response);
}
catch (Exception e)
{
Log.d( "Exception: " , "Yup");
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result)
{
Log.d( "Post execute: " , "In the post-execute method" );
textView.setText(result);
}
}
public void readWebpage(View view)
{
Log.d( "Read webpage: " , "In the read webpage method" );
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "myurl" });
}
}
and when I call it, it does run, hit the remote server and pick up whatever that server outputs. So that is no problem.
The problem is that it creates this NullPointerException after the call is made and the response from the server is returned:
02-23 17:01:44.607: E/AndroidRuntime(2018): FATAL EXCEPTION: main
02-23 17:01:44.607: E/AndroidRuntime(2018): java.lang.NullPointerException
02-23 17:01:44.607: E/AndroidRuntime(2018): at com.problemio.LoginActivity$DownloadWebPageTask.onPostExecute(LoginActivity.java:137)
02-23 17:01:44.607: E/AndroidRuntime(2018): at com.problemio.LoginActivity$DownloadWebPageTask.onPostExecute(LoginActivity.java:1)
02-23 17:01:44.607: E/AndroidRuntime(2018): at android.os.AsyncTask.finish(AsyncTask.java:602)
02-23 17:01:44.607: E/AndroidRuntime(2018): at android.os.AsyncTask.access$600(AsyncTask.java:156)
02-23 17:01:44.607: E/AndroidRuntime(2018): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
02-23 17:01:44.607: E/AndroidRuntime(2018): at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 17:01:44.607: E/AndroidRuntime(2018): at android.os.Looper.loop(Looper.java:137)
02-23 17:01:44.607: E/AndroidRuntime(2018): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-23 17:01:44.607: E/AndroidRuntime(2018): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 17:01:44.607: E/AndroidRuntime(2018): at java.lang.reflect.Method.invoke(Method.java:511)
02-23 17:01:44.607: E/AndroidRuntime(2018): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-23 17:01:44.607: E/AndroidRuntime(2018): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-23 17:01:44.607: E/AndroidRuntime(2018): at dalvik.system.NativeStart.main(Native Method)
Also, what is the most proper thing to do after getting the return call? Should I just make an intent and go to a different Activity screen?
Thanks!
textView.setText(result);inonPostExecutemethod, the textview never init.if you want to notify another activity, you can set data in intent to call another activity, or send broadcast.