編輯:關於Android編程
網上有很多例子來演示Android客戶端和服務器端數據如何實現交互不過這些例子大多比較繁雜,對於初學者來說這是不利的,現在介紹幾種代碼簡單、邏輯清晰的交互例子,本篇博客介紹第二種:
一、服務器端:
代碼1:添加名為“AndroidServerServlet.java”的文件
package com.ghj.packageofservlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AndroidServerServlet extends HttpServlet { private static final long serialVersionUID = 6792396567928634227L; public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { response.setContentType(text/plain; charset=UTF-8); request.setCharacterEncoding(UTF-8); System.err.println(request.getParameter(clientData)); PrintWriter printWriter = response.getWriter(); printWriter.print(您好Android客戶端!); printWriter.flush(); printWriter.close(); } }
代碼2:修改名為“web.xml”的文件
AndroidServerServlet com.ghj.packageofservlet.AndroidServerServlet AndroidServerServlet /AndroidServerServlet
二、Android手機客戶端:
代碼1:添加名為“AndroidClientActivity.java”的文件
package com.example.androidclient; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class AndroidClientActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.android_client); Button sendButton = (Button) findViewById(R.id.send_button); sendButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { new Thread(new Runnable(){ @Override public void run() { HttpPost httpRequest = new HttpPost(http://172.16.99.207:8080/AndroidServer/AndroidServerServlet); Listparams = new ArrayList (); params.add(new BasicNameValuePair(clientData, 您好服務器端!)); try { Message message = new Message(); Bundle bundle = new Bundle(); httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//設置請求參數項 HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpRequest);//執行請求返回響應 if(httpResponse.getStatusLine().getStatusCode() == 200){//判斷是否請求成功 bundle.putString(msg, EntityUtils.toString(httpResponse.getEntity())); }else{ bundle.putString(msg, 沒有獲取到Android服務器端的響應!); } message.setData(bundle); handler.sendMessage(message); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }); } private Handler handler = new Handler(){ @Override public void handleMessage(Message message) { super.handleMessage(message); Bundle bundle = message.getData(); String msg = bundle.getString(msg); Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); } }; }
前言現在的APP大部分需要接入支付功能,而支付的主流就是微信支付和支付寶支付,網上關於微信支付和支付支付資料很多,但是這些資料隨著官方的變動可能變得毫無用處,所以我建議直
先看看效果圖:停在中間自動翻頁序言:最近接到一個任務,做一個類似上面自動翻頁的功能。可以看到,這一屏中有三張圖片顯示出來了,有兩張沒有顯示完全,看到設計圖的時候第一反應是
側滑菜單應用現在非常多,而且實現方式也多種多樣。通過在網上的多方查找,我找到郭霖少俠的這篇文章:http://blog.csdn.net/guolin_blog/arti
前段時間用百度地圖,在使用離線地圖包導入的時候發現一個問題,從百度官網下載的離線包無法導入,但是用百度地圖的demo可以下載離線包使用,當時對方式沒什麼要求,能用就行,就