2 jingjizhixing jingjizhixing 于 2016.04.06 21:38 提问

android和Servlet不能传输数据,但是网络链接已经联通

//servlet的代码
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 Test extends HttpServlet {

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

response.setContentType("text/html");
PrintWriter out = response.getWriter();
String str=request.getParameter("hh");
if(str.equals("hh")){
    out.println("kkkkkkkkkkkkkkkkkkkkkk");
}
out.flush();
out.close();

}

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

doGet(request,response);

}

}
//Android端的代码
package com.example.ceshi;

import org.apache.http.Header;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
haha();
}

public void haha(){
String path = "http://localhost:8080/test/servlet/Test";
RequestParams params = new RequestParams();
params.put("hh","hh");
AsyncHttpClient client = new AsyncHttpClient();
client.post(path, params, new AsyncHttpResponseHandler(){
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2,
Throwable arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "服务器出错!", Toast.LENGTH_LONG).show();
}

    @Override
    public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {

        Toast.makeText(MainActivity.this, "服务器成功!", Toast.LENGTH_LONG).show();
    }

});

}

}
以上的网络链接可以,但是不能进行数据传输。

1个回答

baiyuliang2013
baiyuliang2013   2016.04.07 12:37

可以参考下这篇文章:一个简单的安卓+Servlet图片上传例子:http://blog.csdn.net/baiyuliang2013/article/details/45840817

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!