php - can use httpClient in Android Studio? -


i use httpclient send data php file this

php

<?php     echo $_post['my_data']; ?>  

and add <uses-permission android:name="android.permission.internet" /> androidmanifest.xml connect internet.

this main activity

sendactivity.java

public class sendactivity extends actionbaractivity {      string myjson;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_send);          senddata();     }      public void senddata(){         class getdatajson extends asynctask<string, void, string>{              private progressdialog pdialog;             private inputstream = null;             private string url = "http://----/send.php";             private string page_output = "";              @override             protected string doinbackground(string... args) {                  try {                         // building parameters                 list<namevaluepair> params = new arraylist<namevaluepair>();                 params.add(new basicnamevaluepair("my_data", "this data"));                 // defaulthttpclient                 defaulthttpclient httpclient = new defaulthttpclient();                 httppost httppost = new httppost(url);                 httppost.setentity(new urlencodedformentity(params));                  httpresponse httpresponse = httpclient.execute(httppost);                 httpentity httpentity = httpresponse.getentity();                 = 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, "utf-8"), 8);                 stringbuilder sb = new stringbuilder();                  string line = null;                 while ((line = reader.readline()) != null)                 {                     sb.append(line + "\n");                 }                 is.close();                 page_output = sb.tostring();                  log.i("log", "page_output --> " + page_output);             } catch (exception e) {                 log.e("buffer error", "error converting result " + e.tostring());             }              return page_output;         }              @override             protected void onpostexecute(string result){                 log.i("log", " onpostexecute -> " + result );                 myjson=result;                 log.i("log", "myjson" + myjson);             }         }         getdatajson g = new getdatajson();         log.i("log", " getdatajson " );         g.execute();     } } 

i use android studio , lots of code deprecated , data not send php , can't data php

is httpclient expired android studio or i'm wrong?

http client deprecated in api level 22. must use open openurlconnection. can use code

public class fetchurl {      private url url;      public string fetchurl(string urlstring, hashmap<string, string> values) {         string response = "";         try {             url = new url(urlstring);             log.d("url string", urlstring);             httpurlconnection conn = (httpurlconnection) url.openconnection();             conn.setreadtimeout(15000);             conn.setconnecttimeout(15000);             conn.setrequestmethod("post");             conn.setdoinput(true);             conn.setdooutput(true);              outputstream os = conn.getoutputstream();             bufferedwriter writer = new bufferedwriter(new outputstreamwriter(                     os, "utf-8"));             writer.write(getpostdatastring(values));              writer.flush();             writer.close();             os.close();             int responsecode = conn.getresponsecode();              if (responsecode == httpsurlconnection.http_ok) {                 string line;                 bufferedreader br = new bufferedreader(new inputstreamreader(                         conn.getinputstream()));                 while ((line = br.readline()) != null) {                     response += line;                 }             } else {                 response = "";                  throw new exception(responsecode + "");             }         } catch (exception e) {             e.printstacktrace();         }          return response;     }      private string getpostdatastring(hashmap<string, string> params)             throws unsupportedencodingexception {         stringbuilder result = new stringbuilder();         boolean first = true;         (map.entry<string, string> entry : params.entryset()) {             if (first)                 first = false;             else                 result.append("&");              result.append(urlencoder.encode(entry.getkey(), "utf-8"));             result.append("=");             result.append(urlencoder.encode(entry.getvalue(), "utf-8"));         }         log.d("query string", result.tostring());         return result.tostring();     }  } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -