I am sending Https request to mysql server but not getting the response while performing the same task on localhost it is working. I have no experience using Https, please give me solution and explain difference between Http and Https request sending to mysql server, hope someone help me thanks in advance.
This is my code
protected String doInBackground(String... params) {
String emailSend = params[0];
String tokenSend = params[1];
String deviceIdSend = params[2];
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", emailSend));
nameValuePairs.add(new BasicNameValuePair("token", tokenSend));
nameValuePairs.add(new BasicNameValuePair("deviceId", deviceIdSend));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://xyz/xyz/xyz.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
result = sb.toString();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "success";
}
#Override
protected void onPostExecute(String resultN) {
super.onPostExecute(resultN);
String s = resultN.trim();
//dialog.dismiss();
if (s.equalsIgnoreCase("success")) {
if (result.contains("Pass")) {
Related
I am trying to maintain the login info with session in android.
I am sure that all of my codes on the server are OK cause I have tested them with normal web browsers and also the android webview.
I have checked lots of similar questions on stackoverflow but none have worked for me so far.
here is what I have done...
#Override
protected String doInBackground(String... urls) {
try {
//DefaultHttpClient httpClient = new DefaultHttpClient();
HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpPost httpPost = new HttpPost(urls[0]);
httpPost.setEntity(new UrlEncodedFormEntity(params));
//HttpResponse httpResponse = httpClient.execute(httpPost);
HttpResponse httpResponse = httpClient.execute(httpPost, httpContext);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
int ch;
while ((ch = reader.read()) != -1)
sb.append((char) ch);
Content = sb.toString();
} catch (IOException e) {
Log.e("JsonError", e.getMessage());
}
return Content;
}
#Override
protected void onPostExecute(String page_output) {
Dialog.dismiss();
try {
Log.i("data", page_output);
TextView tv = (TextView) context.findViewById(R.id.textView);
tv.setText(Content);
} catch (Exception e) {
e.printStackTrace();
}
}
can you please help me with this?
I am in real trouble. I spent 8 hours on it, but failed. I want to receive json from server using email as a criteria. Here is working part of code in java script: http://jsfiddle.net/7ydnw3ty/1/
When i try to convert it in android, i get empty result. Please let me know, where is the issue. Please, Please, get me out of trouble.
Here is snippet that is calling.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("authtoken", "61d40d3b0395f730d8e24e77cfada8d7"));
nameValuePairs.add(new BasicNameValuePair("zc_ownername","raffbr2"));
nameValuePairs.add(new BasicNameValuePair("criteria", "'Email==\"teste1#gmail.com\"'"));
nameValuePairs.add(new BasicNameValuePair("scope","creatorapi"));
pBar = new ProgressDialog(MainActivity.this);
pBar.setCanceledOnTouchOutside(false);
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pBar.setMessage("Communication With Server, Registering...");
pBar.show();
MyAsyncTask1 mTask = new MyAsyncTask1();
mTask.setOnResultsListener(MainActivity.this);
mTask.execute(getResources().getString(R.string.readserverUrl)+"LoginSecurity1"
, nameValuePairs);**
Async task fot background processing
public class MyAsyncTask1 extends AsyncTask<Object, Integer, String> {
ResultsListener listener;
public void setOnResultsListener(ResultsListener listener) {
this.listener = listener;
}
#Override
protected String doInBackground(Object... params) {
// TODO Auto-generated method stub
String res = null;
DefaultHttpClient responseBody = null;
if (params.length > 0) {
String mUrl = params[0].toString();
List<NameValuePair> mParams = (List<NameValuePair>) params[1];
try{
DefaultHttpClient httpclient= new DefaultHttpClient();
HttpPost httpget = new HttpPost(mUrl);
httpget.setEntity(new UrlEncodedFormEntity(mParams, "UTF-8"));
HttpResponse response = httpclient.execute(httpget);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = in.readLine()) != null) {
res=line;
System.out.println(line);
}
in.close();
}catch(Exception e){
return res=e.toString();
}
}
return res;
}
#Override
protected void onPostExecute(String result) {
//Toast.makeText(getBaseContext(), result, Toast.LENGTH_SHORT).show();
if (!listener.equals(null)) {
listener.onResultsSucceeded(result);
}
}
public HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
}
I recommend use Volley lib to solve JSON POST or GET tasks :
Volley example
I have this code block that sends a post request to one of my localhost ports:
public String request(String name){
String responseString = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userName", name));
params.add(new BasicNameValuePair("passWord","123455"));
HttpPost post = new HttpPost("http://localhost:2332/getData/postData");
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
post.setEntity(new StringEntity(URLEncodedUtils.format(params,"UTF-8"), HTTP.UTF_8));
responseString = execute(post,params.toString());
return responseString;
}
public String execute(HttpRequestBase requestBase, String params){
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = null;
String responseString = "";
try {
LOG.info("Request Method:{}",requestBase.getMethod());
LOG.info("Request Parameters:{}",params);
response = httpClient.execute(requestBase);
HttpEntity entity = response.getEntity();
responseString = EntityUtils.toString(entity);
} catch (IOException e) {
e.printStackTrace();
}
return responseString;
}
I think that this code block should work because I used a tutorial as a reference but whenever I run my application the value of responseString is null or the application doesn't show me any results. Is there something wrong with my code?
The following Code is not able to connect to SQLite.
public class MainActivity extends ListActivity {
JSONArray jArray; String result = null; InputStream is = null;
StringBuilder sb=null;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>(); //http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx/");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
} //paring data
int Ravid_id;
String Ravid_Name;
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
Ravid_id=json_data.getInt("Ravid_id");
Ravid_Name=json_data.getString("Ravid_Name");
}
} catch(JSONException e1){
Toast.makeText(getBaseContext(), "No found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
This is just a guess and related to an often posted issue: you should not perform any network operations on the main thread. This code ...
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx/");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
... should be in placed in an AsncTask or Service. See this post for an example how to fix this issue (usually an android.os.NetworkOnMainThreadException).
I need to send http POST request from mobile android application to the server side applcation.
This request need to contain json message in body and some key-value parametres.
I am try to write this method:
public static String makePostRequest(String url, String body, BasicHttpParams params) throws ClientProtocolException, IOException {
Logger.i(HttpClientAndroid.class, "Make post request");
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(body);
httpPost.setParams(params);
httpPost.setEntity(entity);
HttpResponse response = getHttpClient().execute(httpPost);
return handleResponse(response);
}
Here i set parametres to request throught method setParams and set json body throught setEntity.
But it isn't work.
Can anybody help to me?
You can use a NameValuePair to do this..........
Below is the code from my project where I used NameValuePair to sent the xml data and receive the xml response, this will provide u some idea about how to use it with JSON.
public String postData(String url, String xmlQuery) {
final String urlStr = url;
final String xmlStr = xmlQuery;
final StringBuilder sb = new StringBuilder();
Thread t1 = new Thread(new Runnable() {
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlStr);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
1);
nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d("Vivek", response.toString());
HttpEntity entity = response.getEntity();
InputStream i = entity.getContent();
Log.d("Vivek", i.toString());
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s = br.readLine()) != null) {
Log.d("YumZing", s);
sb.append(s);
}
Log.d("Check Now",sb+"");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Getting from Post Data Method "+sb.toString());
return sb.toString();
}