你好,我試圖獲得一個簡單的應用程序運行在模擬器上,它將數據發送到運行在同一臺PC上的servlet(http:// 10.0.2.2:8080/。 ..)作爲IP連接到主機PC從android應用程序通信到動態web servlet
附加閱讀:這是一個princable的測試,希望可以讓我在手機上運行應用程序並連接到運行在家中的servlet,最終將用於女巫連接到一個mysql數據庫。 我有一些代碼,似乎做我想做什麼,但我不知道它的儀式或者,如果我正確地執行它
/** Android的側面**/
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項目**/
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沒有同時運行應用程序請求連接或類似的東西
是否有明顯的原因,它不工作和我怎樣才能解決這一問題,這樣我就可以繼續使用在手機上測試應用程序連接到該servlet 任何幫助,將不勝感激,我希望成爲活躍在這一職務,直到我解決這個問題
當您嘗試連接到網址時,您是否在android應用程序中發現任何異常? – Peshal
謝謝您的回覆,但我決定使用JSON將數據發送到servlet而不是流,並且它可以正常工作。我可以把我的代碼作爲答案? – ASH