https calling code from android
I have written this code but still not running
public class MainActivity extends Activity {
this is the activity class
String URL = "";
String result = "";
String deviceId = "xxxxx" ;
final String tag = "Your Logcat tag: ";
private EditText password;
private EditText username;
private Button btnSubmit;
public HttpClient hc;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
password = (EditText) findViewById(R.id.password);
username = (EditText) findViewById(R.id.username);
btnSubmit = (Button) findViewById(R.id.h);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, username.getText(),
Toast.LENGTH_SHORT).show();
/*HttpClient httpClient = new DefaultHttpClient();
httpClient =new MainActivity().sslClient(httpClient);
setHttp(httpClient);
new LongRunningGetIO().execute(getHttp());*/
HttpClient httpClient = getNewHttpClient();
new LongRunningGetIO().execute(httpClient);
this execute method is for running backround operations
}
});
}
protected void setHttp(HttpClient h) {
// TODO Auto-generated method stub
hc=h;
}
protected HttpClient getHttp()
{
return hc;
}
an inner class used fro async operations
private class LongRunningGetIO extends AsyncTask {
private Logger logger = Logger.getLogger("LongRunningGetIO");
protected String getASCIIContentFromEntity(HttpEntity
entity) throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n>0) out.append(new String(b, 0, n));
}
return out.toString();
}
@Override
protected String doInBackground(HttpClient... params) {
JSONObject obj = null;
try{
/*RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new
HttpComponentsClientHttpRequestFactory(HttpUtils.getNewHttpClient()));
String url =
"https://demo.yoursuppliernetwork.com/tnvrs/userservice/getUsers?loginId=qbbuyer1@gmail.com&companyId=YSNCY1353588654343";
obj = restTemplate.getForObject(url, JSONObject.class);
System.out.println(obj);*/
String url =
"https://demo.yoursuppliernetwork.com/tnvrs/userservice/getUsers?loginId=qbbuyer1@gmail.com&companyId=YSNCY1353588654343";
HttpClient httpClient = params[0];
HttpResponse resp = httpClient.execute(new HttpGet(url));
String str = EntityUtils.toString(resp.getEntity());
System.out.println(str);
}
catch(Exception e){
e.printStackTrace();
}
return obj.toString();
}
protected void onPostExecute(String results) {
if (results!=null) {
Button b = (Button)findViewById(R.id.h);
b.setClickable(true);
}
}
}
public class MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public MySSLSocketFactory(KeyStore truststore) throws
NoSuchAlgorithmException, KeyManagementException,
KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[]
chain, String authType) throws
CertificateException {
}
public void checkServerTrusted(X509Certificate[]
chain, String authType) throws
CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
}
public MySSLSocketFactory(SSLContext context) throws
KeyManagementException, NoSuchAlgorithmException,
KeyStoreException, UnrecoverableKeyException {
super(null);
sslContext = context;
}
@Override
public Socket createSocket(Socket socket, String host,
int port, boolean autoClose) throws IOException,
UnknownHostException {
return
sslContext.getSocketFactory().createSocket(socket,
host, port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
private HttpClient sslClient(HttpClient client) {
try {
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new MySSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = client.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", ssf, 443));
return new DefaultHttpClient(ccm, client.getParams());
} catch (Exception ex) {
return null;
}
}
public HttpClient getNewHttpClient() {
try {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("http",
SSLSocketFactory.getSocketFactory(), 443));
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 6000);
HttpConnectionParams.setSoTimeout(params, 6000);
HttpConnectionParams.setSocketBufferSize(params, 8192);
HttpClientParams.setRedirecting(params, false);
final ClientConnectionManager con = new
ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(con, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
}
i have searched on all posts of stackoverflow but still not https not
running.
No comments:
Post a Comment