从Android应用程序到动态Web servlet的通信

时间:2014-02-14 18:38:26

标签: java android mysql jsp servlets

您好我正在尝试在模拟器上运行一个简单的应用程序,该模拟器将数据发送到在同一台PC上运行的servlet,使用(http:// 10.0.2.2:8080 / ...)作为ip连接到主机电脑

补充阅读:这是对可打印的测试,希望能够让我在手机上运行应用程序并连接到在家运行的servlet,最终将用于连接到mysql数据库。 我有一些代码,似乎做我想要的,但我不确定它是否是仪式或我是否正确实施

/ ** Android Side ** /

import java.io.DataOutputStream; 
import java.io.OutputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView;
import android.widget.Toast;

/**adapted code from online**/
public class Test extends Activity { 
     static final String TAG = "Test"; 
    // Called when the activity is first created.  

    protected static EditText username; 
    protected static EditText password;
    TextView tview;
    String uname ="";

    @Override 
    public void onCreate(Bundle icicle) { 
        super.onCreate(icicle); 
        setContentView(R.layout.testlogin); 

        username = (EditText)findViewById(R.id.username); 
        password = (EditText)findViewById(R.id.password);
        tview = (TextView)findViewById(R.id.view);
        Toast.makeText(this, "started",1).show();
        tview.append("\nstarted\n"); 
    } 

    public void login(View v) { //in testlogin.xml//android:onClick="login"
        Toast.makeText(this, "button pressed",1).show();
        Log.v(TAG, "button pressed");
        //tview.append("clicked\n");
        uname=username.getText().toString(); 
        networkthread ob = new networkthread(uname); 
    } 
}

class networkthread implements Runnable { 
        private static final String TAG = "Test"; 
        String uname; 
        public networkthread(String uname) { 
                this.uname =  uname; 
                Thread t = new Thread(this); 
                t.start(); 
        } 
        public void run(){


                Log.v(TAG, "inside sub thread"); 

                try { 
                    URL ob = new URL("http://10.0.2.2:8080/Server/test"); 
                    URLConnection           conn = ob.openConnection(); 
                    conn.setDoInput(true); 
                    conn.setDoOutput(true); 
                    OutputStream out = conn.getOutputStream(); 
                    DataOutputStream dos =  new DataOutputStream(out); 
                    dos.writeInt(uname.getBytes().length); 
                    dos.write(uname.getBytes(),0,uname.getBytes().length); 
                    dos.flush(); 
                    dos.close(); 
                    conn = null; 
                }catch(Exception e){ 
                        Log.v(TAG, "Exception:" +e); 
                        e.printStackTrace(); 
                } 
        } 
} 

/ **动态Web项目中的Servlet端** /

import java.io.*; 
import java.net.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 

public class test extends HttpServlet { 
    private InputStream is = null; 
    private OutputStream os = null; 
    private DataInputStream dis = null; 
    private DataOutputStream dos = null; 
    boolean isValid = false; 

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        try{ 
            is = request.getInputStream(); 
            dis = new DataInputStream(is); 
            int  len = dis.readInt(); 
            byte data[] = new byte[len]; 
            dis.read(data,0,len); 
            String userName = new String(data); 
            System.out.println("username:" +userName); 
            is.close(); dis.close(); 
        } catch(Exception e){ 
            System.out.println(e); 
        } 
    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        processRequest(request, response); 
    } 


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        processRequest(request, response); 
    } 


    public String getServletInfo() { 
        return "Short description"; 
    } 

}

我对android和servlet都有一些经验 当我运行它时没有错误但是servlet端没有恢复消息。我想我可能与应用程序有关,不等待servlet的响应或servlet在应用程序请求连接或类似的东西的同时没有运行

是否有明显的原因导致它无法正常工作以及如何解决此问题,以便我可以继续使用手机上的测试应用程序连接到servlet 任何帮助将不胜感激,我希望在这个帖子上活跃,直到我解决问题

1 个答案:

答案 0 :(得分:1)

应用

package com.test;

import java.io.BufferedReader;
import java.io.DataOutputStream; 
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream; 
import java.net.HttpURLConnection;
import java.net.URL; 
import java.net.URLConnection; 


import java.util.HashMap;
import java.util.logging.Logger;

import javax.net.ssl.HttpsURLConnection;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity; 
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.AsyncTask;
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu;
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView;
import android.widget.Toast;


public class Test extends Activity { 
    static final String TAG = "Test"; 
    // Called when the activity is first created.  

    protected static EditText username; 
    protected static EditText password;
    TextView tview;
    String uname ="";
    JSONObject json;
    URL url;

    @Override 
    public void onCreate(Bundle icicle) { 
        super.onCreate(icicle); 
        setContentView(R.layout.testlogin); 

        username = (EditText)findViewById(R.id.username); 
        password = (EditText)findViewById(R.id.password);
        tview = (TextView)findViewById(R.id.view);
        Toast.makeText(this, "Started",1).show();
        Log.v(TAG,"\nStarted\n"); 
    } 

    public void login(View v) { //in testlogin.xml//android:onClick="login"
        Toast.makeText(this, "button pressed",1).show();
        Log.v(TAG, "button pressed");
        //tview.append("clicked\n");

        HashMap<String, String> hasmap=new HashMap<String, String>();
        hasmap.put("user",username.getText().toString());
        hasmap.put("pass",password.getText().toString());
        json = new JSONObject(hasmap);
        String sJSON = json.toString();
        new toServlet().execute(sJSON);//sends the jsonString to a aSync task
    }

    private class toServlet extends AsyncTask<String, Void, String> {
        @Override

        protected String doInBackground(String... urls) {

            // params comes from the execute() call: params[0] is the url.
            try {
                Log.v(TAG,"In background");
                return connection(urls[0]);//could send message to multible uls
            } catch (IOException e) {
                Log.v(TAG,"Error: "+e);
                return "Unable to retrieve web page. URL may be invalid.";
            }
        }
    }
    private String connection (String sJSON)throws IOException{//creates new connection each time
        Log.v(TAG, "Connection");
        Log.v(TAG,"In connection "+sJSON);
        URL url;
        InputStream is = null;
        HttpURLConnection conServlet;

        url = new URL("http://10.0.2.2:8080/CheckmateServerSuport/servlet?TAG="+sJSON);
        conServlet = (HttpURLConnection) url.openConnection();
        conServlet.setDoInput(true);
        //con.setDoInput(true);
        conServlet.setRequestMethod("POST");//can be get

        conServlet.connect();//makes the connection and send


        //Response
        is = conServlet.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        Log.v(TAG,"Sent String ");

        String back = br.readLine();
        Log.v(TAG,""+back);

        return back;

    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

的servlet

package com.coreservlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.AbstractDocument.BranchElement;

import org.json.JSONException;
import org.json.JSONObject;

@WebServlet("/servlet")
public class JSONServlet extends HttpServlet{

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        String user ="";
        String pass = "";

        System.out.println("In do Get");
        response.setContentType("application/json");

        try {
            JSONObject jsonRequest = new JSONObject(request.getParameter("TAG"));//from url in app
            System.out.println(jsonRequest);
            user = jsonRequest.getString("user");
            pass = jsonRequest.getString("pass");

            System.out.println("UserName: "+user+" Password: "+pass);

            //send back to app
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ok","ok");

            JSONObject replyJSON = new JSONObject(map);

            PrintWriter printW = response.getWriter();
            printW.println(replyJSON);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//essentioall dummy
        doGet(request, response);
    }
}