1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
public class TestWebService extends Activity {
/** Called when the activity is first created. */
String response;
final String username = "android";
String result;
final String password = "Android85";
String url = "https://10.0.2.2/webapp/wcs/stores/servlet/TrackOrderStatus?storeId=10101&catalogId=10101&langId=-2";
// 10.0.2.2
HttpEntity entity = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// DefaultHttpClient httpClient = new DefaultHttpClient();
Context context= null;
DefaultHttpClient client = new MyHttpClients(context);
AuthScope authScope = new AuthScope(null, -1);
Credentials credential = new UsernamePasswordCredentials(username,password);
client.getCredentialsProvider().setCredentials(authScope, credential);
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("User-Agent", "Android");
HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
HttpResponse httpresponse = null;
try {
httpresponse = client.execute(httpGet);
} catch (ClientProtocolException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
} catch (IOException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
HttpEntity entity = httpresponse.getEntity();
InputStream chaine = null;
try {
chaine = entity.getContent();
} catch (IllegalStateException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
result = stream2String(chaine);
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
JSONArray jsonOrdArray = null;
try {
jsonOrdArray = new JSONArray(result);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (int i = 0; i < jsonOrdArray.length(); i++) {
Order order = new Order();
JSONObject commande = null;
try {
commande = jsonOrdArray.getJSONObject(i);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
order.reference = commande.getString("reference");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private String stream2String(InputStream stream)
throws UnsupportedEncodingException {
String charSet = "iso-8859-1";
InputStreamReader reader = new InputStreamReader(stream, charSet);
BufferedReader buffer = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
try {
String cur;
while ((cur = buffer.readLine()) != null) {
sb.append(cur);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
} |
Partager