i have problem on asychronous http post, I've managed to send data from EditText to the server if run using the emulator. but fails when using a real device. how to be able to send data from android studio to the server(xampp) if run using tge real device?
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText value;
private Button btn;
private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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 void onClick(View v) {
if (value.getText().toString().length()<1){
Toast.makeText(getApplicationContext(),"Isi sesuatu Dong!",Toast.LENGTH_LONG).show();
}else{
pb.setVisibility(View.VISIBLE);
}
}
private class MyAsyncTask extends AsyncTask<String,Integer,Double>{
@Override
protected Double doInBackground(String... params) {
postData(params[0]);
return null;
}
protected void onPostExecute(Double hasil) {
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),"Berhasil dikirim",Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Integer... progress) {
pb.setProgress(progress[0]);
}
public void postData(String nilaiYangDikirim){
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/http/index.php");
try {
List<NameValuePair> nilai = new ArrayList<NameValuePair>();
nilai.add(new BasicNameValuePair("myHttpData", nilaiYangDikirim));
httpPost.setEntity(new UrlEncodedFormEntity(nilai));
HttpResponse response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}