I am newbie to native android development. I have created a DB
in phpmyadmin
using xampp
server. Then i created a REST webservice
using Yii
. I tested the web service on ARC
and it's working fine. Now i want to test it on my device. So i searched many articles and found a answer (link). But it doesn't helped me out. Below is my code
public class MainActivity extends AppCompatActivity {
String URLGET = "http://192.168.8.85:8000/app/web/users/";
String result = "";
TextView getData;
EditText userInput;
public static final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getData = (TextView) findViewById(R.id.getData);
userInput = (EditText) findViewById(R.id.EnterId);
final Button button = (Button) findViewById(R.id.btn_Submit);
button.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String reqURL = URLGET;
new RestOperation().execute(reqURL);
//callWebService(query);
}
});
}
public class RestOperation extends AsyncTask<String, Void, Void> {
final HttpClient httpClient = new DefaultHttpClient();
String content;
String error;
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
String data = "";
// TextView getData = (TextView) findViewById(R.id.getData);
// EditText userInput = (EditText) findViewById(R.id.EnterId);
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setTitle("Please Wait...");
progressDialog.show();
data = "data=" + URLEncoder.encode(String.valueOf(userInput.getText()));
}
@Override
protected Void doInBackground(String... params) {
BufferedReader br = null;
URL url = null;
try {
url = new URL(params[0]);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
outputStreamWriter.write(data);
outputStreamWriter.flush();
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
content = sb.toString();
} catch (MalformedURLException e) {
error = " Exception: " + e.getMessage();
e.printStackTrace();
} catch (IOException e) {
error = " IOException: " + e.getMessage();
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
if (error != null) {
getData.setText("Error" + error);
} else {
getData.setText(content);
}
}
}}
The above ip
address is my wireless address. Firstly i was testing it on emulator using localhost
but on searching i found that i should use 10.0.2.2
. So i use it and it still doesn't works. I gives me the a warning
in logcat
as shown below
I must be missing some thing that i don't know. I am stuck to it from almost 2-3 days and really don't know what to do.
Any help would be highly appreciated