Add the following jars in your /lib folder
------------------------------------------
spring-android-auth-1.0.0.M3.jar
spring-android-core-1.0.0.M3.jar
spring-android-rest-template-1.0.0.M3.jar
MainActivity.java
--------------------
package com.kundan.springforandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
static TextView tv;
public static final String URL="http://api.androidhive.info/contacts/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONParser parser=new JSONParser();
parser.execute(new String[]{URL});
tv=(TextView) findViewById(R.id.springmessage);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
------------------------------------------
spring-android-auth-1.0.0.M3.jar
spring-android-core-1.0.0.M3.jar
spring-android-rest-template-1.0.0.M3.jar
MainActivity.java
--------------------
package com.kundan.springforandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
static TextView tv;
public static final String URL="http://api.androidhive.info/contacts/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONParser parser=new JSONParser();
parser.execute(new String[]{URL});
tv=(TextView) findViewById(R.id.springmessage);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
JSONParse.java
-------------------
package com.kundan.springforandroid;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import android.os.AsyncTask;
public class JSONParser extends AsyncTask<String, Void, String> {
String output;
@Override
protected String doInBackground(String... urls) {
for(String url:urls){
output=getOutputFromUrl(url);
}
return output;
}
private String getOutputFromUrl(String url) {
try {
HttpHeaders headers=new HttpHeaders();
headers.setContentType(new MediaType("text","xml"));
HttpEntity<String>requestEntity=new HttpEntity<String>(headers);
RestTemplate restTemplate=new RestTemplate();
ResponseEntity<String>responseEntity=restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
String output=responseEntity.getBody();
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
@Override
protected void onPostExecute(String output) {
MainActivity.tv.setText(output);
}
}
package com.example.apitest;
ReplyDeleteimport java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Application;
import android.util.Log;
public class WebService {
// private String _apiUrl;
private final static int TIMEOUT_MILLISEC = 40000;
// public void setApiUrl(String _apiUrl) {
// this._apiUrl = _apiUrl;
// }
//
// public WebService(String apiUrl){
// setApiUrl(apiUrl);
// }
public String excutePost(String _apiUrl,String urlParameters)
{
URL url;
HttpURLConnection connection = null;
try {
//Create connection
urlParameters = "REQUEST=" +urlParameters;
//System.out.println(urlParameters);
Log.e("WebServiceRequest", urlParameters);
url = new URL(_apiUrl);
Log.e("WebServiceIP", url+"");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setConnectTimeout(TIMEOUT_MILLISEC);
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
Log.e("WebServiceResponse", response.toString());
// System.out.println(response);
// fun(response.toString());
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}