I have this script.
I have gone through many variations of the problem on stack overflow and used the solution to try and build the knowledge to do this but it seems to be failing everytime, can someone help?
public class Main extends Activity implements OnClickListener {
private EditText value;
private Button btn;
private ProgressBar pb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
value = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
pb = (ProgressBar) findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (value.getText().toString().length() < 1) {
// out of range
Toast.makeText(this, "please enter something", Toast.LENGTH_LONG)
.show();
} else {
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute("hey");
}
}
private class MyAsyncTask extends AsyncTask<String, Integer, Double> {
@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0]);
return null;
}
protected void onPostExecute(Double result) {
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "command sent",
Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Integer... progress) {
pb.setProgress(progress[0]);
}
public void postData(String valueIWantToSend) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://users.aber.ac.uk/bym1/group/androidto.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myHttpData",
valueIWantToSend));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}
I found this code on the net and I'm just trying to get it to work so i have a working prototype to work off and build my own but I keep getting this in my log file:
01-28 22:30:07.030: W/dalvikvm(27142): threadid=11: thread exiting with uncaught exception (group=0x40bc2498)
01-28 22:30:07.030: E/test(27142): Exception
01-28 22:30:07.060: E/AndroidRuntime(27142): FATAL EXCEPTION: AsyncTask #1
01-28 22:30:07.060: E/AndroidRuntime(27142): java.lang.RuntimeException: An error occured while executing doInBackground()
01-28 22:30:07.060: E/AndroidRuntime(27142): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-28 22:30:07.060: E/AndroidRuntime(27142): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.lang.Thread.run(Thread.java:856)
01-28 22:30:07.060: E/AndroidRuntime(27142): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-28 22:30:07.060: E/AndroidRuntime(27142): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-28 22:30:07.060: E/AndroidRuntime(27142): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-28 22:30:07.060: E/AndroidRuntime(27142): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-28 22:30:07.060: E/AndroidRuntime(27142): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-28 22:30:07.060: E/AndroidRuntime(27142): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-28 22:30:07.060: E/AndroidRuntime(27142): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-28 22:30:07.060: E/AndroidRuntime(27142): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-28 22:30:07.060: E/AndroidRuntime(27142): at com.example.httpasync.Main$MyAsyncTask.postData(Main.java:98)
01-28 22:30:07.060: E/AndroidRuntime(27142): at com.example.httpasync.Main$MyAsyncTask.doInBackground(Main.java:70)
01-28 22:30:07.060: E/AndroidRuntime(27142): at com.example.httpasync.Main$MyAsyncTask.doInBackground(Main.java:1)
01-28 22:30:07.060: E/AndroidRuntime(27142): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-28 22:30:07.060: E/AndroidRuntime(27142): ... 5 more
01-28 22:30:07.060: E/AndroidRuntime(27142): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
01-28 22:30:07.060: E/AndroidRuntime(27142): at libcore.io.Posix.getaddrinfo(Native Method)
01-28 22:30:07.060: E/AndroidRuntime(27142): at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
01-28 22:30:07.060: E/AndroidRuntime(27142): at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
01-28 22:30:07.060: E/AndroidRuntime(27142): ... 19 more
01-28 22:30:07.060: E/AndroidRuntime(27142): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
01-28 22:30:07.060: E/AndroidRuntime(27142): ... 22 more
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.httpasync"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.httpasync.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Internet permissions are set, I have looked up the exceptions but the answers dont seem to relate to my code, any ideas?
to post using asychtask can done by follow
httphandler class
add dependency as follow
The clue is in the logcat:
You are missing
android.permission.INTERNET
permission in your AndroidManifest. Add this line:just below:
try with this way .
String BASEURL = "http://androidexample.com/media/webservic/JsonReturn.php";
Gradle file changes
// add on list
ListView listview; ArrayList mylistname=new ArrayList<>(); ArrayList mylistnum=new ArrayList<>();
mylistname.add("amit"); mylistnum.add("7042757424");
// adapter set data items public class MyArrayAdapter implements ListAdapter { Activity activity; int size; ArrayList mylistname,mylistnum;
}