Android URLconnection
生活随笔
收集整理的這篇文章主要介紹了
Android URLconnection
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
#使用Thread請(qǐng)求HTTP # 普通的使用java的Thread 線程,在重寫run 方法,實(shí)現(xiàn)HTTPConnection
public class DoubleMeActivity extends Activity implements OnClickListener {EditText inputValue=null;Integer doubledValue =0;Button doubleMe;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.calculate);inputValue = (EditText) findViewById(R.id.inputNum);doubleMe = (Button) findViewById(R.id.doubleme);doubleMe.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.doubleme:new Thread(new Runnable() {public void run() {try{URL url = new URL("http://10.0.2.2:8080/MyServletProject/DoubleMeServlet");URLConnection connection = url.openConnection();String inputString = inputValue.getText().toString();//inputString = URLEncoder.encode(inputString, "UTF-8");Log.d("inputString", inputString);connection.setDoOutput(true);OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());out.write(inputString);out.close();BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String returnString="";doubledValue =0;while ((returnString = in.readLine()) != null) {doubledValue= Integer.parseInt(returnString);}in.close();runOnUiThread(new Runnable() {public void run() {inputValue.setText(doubledValue.toString());}});}catch(Exception e){Log.d("Exception",e.toString());}}}).start();break;}}}轉(zhuǎn)載于:https://my.oschina.net/541996928/blog/407472
總結(jié)
以上是生活随笔為你收集整理的Android URLconnection的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (LeetCode 141/142)Li
- 下一篇: liunx系统内核安装图形化界面