I've googled for that topics and don't get any useful information.
I want to use Web API in my android projects, but don't know how to call them from android or java
I've got some WEB API from this site and want to use in my android project.
For example, a web api URL : /api/uname2uid/{uname}.
that convert User name to User numeric ID
I want to display the User ID returned from that API into a TextView. So i need the return value as string or number. Please help me this.
EDIT
I just want to learn how to use WEB API
For more simplicity:
In my program I have a TextView, aEditText and a Button
When i press the Button then TextView will update by corresponding user ID for Username given in EditText field thats all. An working example will be great for me to understanding this.
A simple Async call would do the thing for you.:
MainActivity.java
new YourAsync(){
protected void onPostExecute(Object[] result) {
String response = result[1].toString();
try{
JSONObject jsonObject=(new JSONObject(jsonResult)).getJSONObject("data"); //Parsing json object named 'data'
yourTextView.setText(jsonObject.getString("uname"));
}.execute(YourURL);
YourAsync.java
public class YourAsync extends AsyncTask<String, Integer, Object[]> {
@Override
protected Object[] doInBackground(String... params) {
// TODO Auto-generated method stub
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0].toString());
Log.d("http client post set","");
try{
HttpResponse response = httpclient.execute(httppost);
Log.d("YourAsync","Executed");
return new Object[]{response, new BasicResponseHandler().handleResponse(response)};
}catch(Exception e){
Log.d("" + e, "");
}
return new Object[0];
}
public class JsonWebServiceCall {
// HttpURLConnection method to get JSON data from Webservice
public static String GetJsonString(String mUpdateUrl) {
String result=null;
try {
URL url = new URL(mUpdateUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
con.setAllowUserInteraction(false);
con.setInstanceFollowRedirects(true);
// con.setRequestMethod("POST");
con.connect();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
// BufferedReader bufferedReader = new BufferedReader(new
// InputStreamReader(con.getInputStream()));
try {
InputStream in = new BufferedInputStream(con.getInputStream());
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(in));
String json;
while ((json = bufferedReader.readLine()) != null) {
System.out.println(mUpdateUrl + "kkkkkkkkk------" + json);
sb.append(json + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
Log.i("json", "===" + sb.toString().trim());
result=sb.toString().trim();}
else{
result=null;
}
return result;
} catch (Exception e) {
return null;
}
}
// HttpURLConnection method to get JSON data from Webservice
public static String PostJsonString(String mUpdateUrl, String webserviceurl) {
// BufferedReader bufferedReader = null;
try {
String urlParameters = mUpdateUrl;
URL url = new URL(webserviceurl);
URLConnection conn = url.openConnection();
StringBuilder sb = new StringBuilder();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(
conn.getOutputStream());
writer.write(urlParameters);
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
sb.append(line + "\n");
}
writer.close();
reader.close();
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
Asynchtask..........
public static class AsyncCriticalMessage extends AsyncTask<Void, Void, Void> {
String result = null;
CriticalMessageServiceSuccess criticalMessageServiceSuccess = null;
public AsyncCriticalMessage(CriticalMessageServiceSuccess criticalMessageServiceSuccess) {
this.criticalMessageServiceSuccess = criticalMessageServiceSuccess;
}
@Override
protected Void doInBackground(Void... params) {
result = JsonWebServiceCall.GetJsonString(Globals.FORCEUPDATE_MESSAGE_SERVICE_URL);
return null;
}
@Override
protected void onPostExecute(Void Result) {
super.onPostExecute(Result);
criticalMessageServiceSuccess.onSuccess(result);
}
}
AsyncCriticalMessage asyncCriticalMessage=new AsyncCriticalMessage(this);
asyncCriticalMessage.execute();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With