Reputation: 23
Based on this article (http://theopentutorials.com/tutorials/android/http/android-how-to-send-http-get-request-to-servlet-using-apache-http-client/), made a simple http client. But I can not add parameters to url.
I’m trying to pass a parameter from a text box:
EditText editTextValue
public String givenValue = editTextValue.getText (). toString ();
public final String URL = "http://example.com/script.php?ip =" + givenValue;
But getting this error:
ERROR / AndroidRuntime (7103): FATAL EXCEPTION: main
at com.example.MainActivity. (MainActivity.java: 39) (public String givenValue = editTextValue.getText (). toString () ;)
How can I fix it?
Sorry for my English.
Update:
Full Source:
package com.example.App;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import android.widget.EditText;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
Button btnGetInfo;
TextView textView;
EditText editTextValue;
public String givenValue = editTextValue.getText().toString();
public String URL = "http://example.com/script.php?value=" + givenValue;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewsById();
btnGetInfo.setOnClickListener(this);
}
private void findViewsById() {
btnGetInfo = (Button) findViewById(R.id.buttonGetInfo);
textView = (TextView) findViewById(R.id.textView);
editTextValue = (EditText) findViewById(R.id.editTextValue);
}
public void onClick(View view) {
GetXMLTask task = new GetXMLTask();
task.execute(new String[] {URL});
}
private class GetXMLTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String output = null;
for (String url : urls) {
output = getOutputFromUrl(url);
}
return output;
}
private String getOutputFromUrl(String url) {
String output = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
output = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return output;
}
@Override
protected void onPostExecute(String output) {
textView.setText(output);
}
}
}
And log:
ERROR/AndroidRuntime(8367): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.App/com.example.App.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.App.MainActivity.<init>(MainActivity.java:28)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
... 11 more
Upvotes: 0
Views: 1748
Reputation: 11870
I think your EditText is probably null. Did you setup edittext before calling the getText()? Try:
EditText editTextValue = (EditText) findViewByItd(R.id.yourEditText);
Base on the tutorial, you can put this line in findViewsById.
private void findViewsById() {
button = (Button) findViewById(R.id.button);
outputText = (TextView) findViewById(R.id.outputTxt);
editTextValue = (EditText) findViewById(R.id.editTextValue);
}
Edit : You have to move
public String givenValue = editTextValue.getText().toString();
to somewhere proper.
editTextValue.getText() shouldn't be called before onCreate and findViewById.
Maybe you could try placing the line in onPostExecute or onClick. (Just for test)
Update 2:
Move givenValue and URL to onClick
NOTE: I HAVEN'T TESTED THE FOLLOWING CODE:
package com.example.App;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import android.widget.EditText;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
Button btnGetInfo;
TextView textView;
EditText editTextValue;
public String givenValue;
public String URL;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewsById();
btnGetInfo.setOnClickListener(this);
}
private void findViewsById() {
btnGetInfo = (Button) findViewById(R.id.buttonGetInfo);
textView = (TextView) findViewById(R.id.textView);
editTextValue = (EditText) findViewById(R.id.editTextValue);
}
public void onClick(View view) {
givenValue = editTextValue.getText().toString();
URL = "http://example.com/script.php?value=" + givenValue;
GetXMLTask task = new GetXMLTask();
task.execute(new String[] {URL});
}
private class GetXMLTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String output = null;
for (String url : urls) {
output = getOutputFromUrl(url);
}
return output;
}
private String getOutputFromUrl(String url) {
String output = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
output = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return output;
}
@Override
protected void onPostExecute(String output) {
textView.setText(output);
}
}
}
Upvotes: 1
Reputation: 44571
You have multiple problems here. The first being that you are trying to call getText()
on an EditText
that has not yet been initialized. But even if you had, you would still get the same error because you haven't inflated your layout
yet. You need to call setContentViw()
before trying to access View
s in the layout
. Also, you can't try to access the View
s before onCreate()
has run. It should look something more like the following...
public class MainActivity extends Activity implements OnClickListener{
Button btnGetInfo;
TextView textView;
EditText editTextValue;
public String givenValue = "";
public String URL = "http://example.com/script.php?value=";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewsById(); // its fine to use this method but do it before trying to get the value from the EditText
givenValue = editTextValue.getText().toString();
URL = URL.concat(givenValue); // there are different ways but use one to concatenate the Strings
This method findViewsById()
should work but its awfully close to a native Android function so I would consider renaming it to something like init()
or loadViews()
or something else.
Upvotes: 0