Jump to content

Archived

This topic is now archived and is closed to further replies.

diengkals

authentification a un server distant

Recommended Posts

bonjour , je doit me connecter a un server webphsere pour récupérer mes données pour cela je doit d abord m authenfier(j arrive a recuperer à partir d un web service les données accessible sans authentification mais mon problème se pose au niveau d authentification ) . j ai cherché quelque chose sur le net mais toujours sans suces . je vous met mon code voir si j ai pas fait des erreur de code . merci d avance




public class Authentification extends Activity {
/** Called when the activity is first created. */
String response;
final String username = "19851985";
String result;
final String password = "android85";

HttpResponse httpresponse;
InputStream chaine;

final String proxyHost = "localhost";
final int proxyPort = 80;

String url = "http://10.0.2.2/webapp/wcs/stores/servlet/AddressBookForm?storeId=10101&catalogId=10101&langId=-2";

HttpEntity entity;
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);

	DefaultHttpClient client = new DefaultHttpClient();
	AuthScope authScope = new AuthScope(null,-1, AuthScope.ANY_REALM);
	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 = client.execute(httpGet);
	entity = httpresponse.getEntity();
		chaine = entity.getContent();
		result = stream2String(chaine);


	JSONArray jsonOrdArray =  new JSONArray(result);;


	for (int i = 0; i < jsonOrdArray.length(); i++) {
		Order order = new Order();
		JSONObject commande =  jsonOrdArray.getJSONObject(i);;


			order.reference = commande.getString("reference");

	}



}

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();
}

}

Share this post


Link to post
Share on other sites





×
×
  • Create New...