آموزش برنامه نویسی اندروید

آموزش برنامه نویسی اندروید

آموزش برنامه نویسی اندروید

آموزش برنامه نویسی اندروید

آموزش برنامه نویسی اندروید

پیوندهای روزانه

استفاده از HTTPURLConnection در اندروید

يكشنبه, ۲۰ دی ۱۳۹۴، ۱۲:۴۱ ب.ظ

به نام خدا

سلام.

یکی از تغییرات به گفته #گوگل در اندروید #مارشمالو حذف Apache HTTP client می باشد.
و گوگل پیشنهاد کرده به جای آن ، برنامه نویسان از #HttpURLConnection استفاده کنند. حال ما میخوایم یه مثال کامل و با توضیحات از این متد براتون قرار بدیم:


//import these on your header
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;

//do this wherever you are wanting to POST
URL url;
HttpURLConnection conn;

try{
//if you are using https, make sure to import java.net.HttpsURLConnection
url=new URL(“http://esfandune.ir/somefile.php”);

//you need to encode ONLY the values of the parameters
String param=”param1=” + URLEncoder.encode(“value1″,”UTF-8″)+
“&param2=”+URLEncoder.encode(“value2″,”UTF-8″)+
“&param3=”+URLEncoder.encode(“value3″,”UTF-8″);

conn=(HttpURLConnection)url.openConnection();
//set the output to true, indicating you are outputting(uploading) POST data
conn.setDoOutput(true);
//once you set the output to true, you don’t really need to set the request method to post, but I’m doing it anyway
conn.setRequestMethod(“POST”);

//Android documentation suggested that you set the length of the data you are sending to the server, BUT
// do NOT specify this length in the header by using conn.setRequestProperty(“Content-Length”, length);
//use this instead.
conn.setFixedLengthStreamingMode(param.getBytes().length);
conn.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”);
//send the POST out
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.close();

//build the string to store the response text from the server
String response= “”;

//start listening to the stream
Scanner inStream = new Scanner(conn.getInputStream());

//process the stream and store it in StringBuilder
while(inStream.hasNextLine())
response+=(inStream.nextLine());

}
//catch some error
catch(MalformedURLException ex){
Toast.makeText(MyActivity.this, ex.toString(), 1 ).show();

}
// and some more
catch(IOException ex){

Toast.makeText(MyActivity.this, ex.toString(), 1 ).show();
}


@esfandune

امیدواریم بتونه کمکتون کنه.
همیشه از متدهای جدید استفاده کنید:)
منابع:
دولوپر اندروید:
http://opizo.com/8oQgW8
و:
http://opizo.com/t2FwaS

نظرات (۱)

  • فرهادرحیمی
  • سلام وعرض ادب واحترام دست گلت درد نکنه خیلی بهم کمک کرد
    ممنونم
    ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
    شما میتوانید از این تگهای html استفاده کنید:
    <b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
    تجدید کد امنیتی