Answer the question
In order to leave comments, you need to log in
Reading and sending, how to do it establish an https connection?
Good day,
help me deal with androids, I'm just starting to make friends
, the basis is taken from the Internet
, I need to establish a connection, read and send data.
The server works on https; the client has a certificate; it includes
package com.devcolibri.parser;
import android.content.res.Resources;
import android.util.Log;
import com.devcolibri.androidandmysql.AdditionalKeyStoresSSLSocketFactory;
import com.devcolibri.androidandmysql.R;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;
import java.security.KeyStore;
import java.util.List;
import java.io.IOException;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// метод получение json объекта по url
// используя HTTP запрос и методы POST или GET
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(getResources().openRawResource(R.raw.keystore), "".toCharArray());
SSLSocketFactory sslSocketFactory = new AdditionalKeyStoresSSLSocketFactory(keystore);
HttpParams params1 = new BasicHttpParams();
HttpProtocolParams.setVersion(params1, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params1, HTTP.UTF_8);
HttpProtocolParams.setUseExpectContinue(params1, true);
final SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sslSocketFactory, 3123));
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params1, registry);
DefaultHttpClient httpclient = new DefaultHttpClient(manager, params1);
HttpPost httpPostRequest = new HttpPost("https://your_service");
// datas - array which contains data to send to server
StringEntity se = new StringEntity(datas[0].toString(), HTTP.UTF_8);
// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-Type", "application/json");
HttpResponse response = httpclient.execute(httpPostRequest); // Создаем HTTP запрос
try {
// проверяем метод HTTP запроса
if(method == "POST"){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// пытаемся распарсить строку в JSON объект
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// возвращаем JSON строку
return jObj;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question