开发者

Android client and server using servlet

I'm running my servlet program on a localhost, and my android code is running on emulator in the same system, I want to send some requests from an android client to the servlet and the servlet program should also send back some data to android after getting the request and it should send using response. Please let me know the code.

This is my android code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
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 android.util.Log;

public class CheckHttpClint {
    public static void executeHttpPost() throws IOException {
        BufferedReader bufferedReader = null;
        try {

            HttpClient client = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost("url");

            List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
            String details = "details";
            pairs.add(new BasicNameValuePair("pid", details));
            httpPost.setEntity(new UrlEncodedFormEntity(pairs));
            HttpResponse httpResponse = client.execute(httpPost);

            bufferedReader = new BufferedReader(new InputStreamReader(
                                                    httpResponse.getEntity().getContent()));
            StringBuffer buffer = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separater");
            while ((line = bufferedReader.readLine()) != null) {
                buffer.append(line + NL);
                System.out.println(buffer);
                Log.i("data send.", null);


            }
            bufferedReader.close();
            String page = buffer.toString();
            System.out.println(page);

        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException exception) {
                    exception.printStackTrace();
                }
            }
        }
    }
}


servlet code:
import java.io.*;
import java.util.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DBConnection extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws     IOExce开发者_Go百科ption, ServletException {



        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<body>");

        String pid1 = request.getParameter("pid");
        out.print(pid1);
        out.println("</body></html>");
        out.close();
    }

}


You can find a tutorial for android describing GET, POST and MULTIPART POST here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜