解决方案 »

  1.   

    例如
    new Thread(new Runnable() {
    @Override
    public void run() {
    Map<String, String> params = new HashMap<String, String>();
                        params.put("username", username); }
    }).start();
      

  2.   

    还没打完,不小心提交了
    例如
    string text=null;
    new Thread(new Runnable() {
    @Override
    public void run() {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("username", username);
                        text=HttpUtils.submitPostData(params, "utf-8")}
    }).start();
    //在子线程外提示
    Toast toast = Toast.makeText(LocationActivity.this, text, Toast.LENGTH_SHORT).show();
      

  3.   

    不能在UI线程里访问网络,而且submitPostData返回的值也要注意线程同步,所以只用new Thread来处理也不正确,用Handler来处理没有问题,但是比较繁琐。
    最合适的方式是用AsyncTask处理:public void onClick(View v)
    {
    if (!mLocationClient.isStarted()) {
    return;
    }
    String username = LocationResult.getText().toString();
    new AsyncTask<String, Void, String>() {
            @Override
            public String doInBackground(String... strs) {
    String username = strs[0];
    Map<String, String> params = new HashMap<String, String>();
    params.put("username", username);
    return HttpUtils.submitPostData(params, "utf-8");
    }
    @Override
    public void onPostExecute(String result) {
    Toast toast = Toast.makeText(LocationActivity.this, result);
    toast.show();
    }
    }.execute(username);
    }
      

  4.   


    你的方法可以正确的得到返回值了  但是返回的只是我那个网页处理的源代码的,并没有返回处理后的数据啊……
    返回的是这个<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>    
        <title>用户登录界面</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <meta http-equiv="content-type" content="text/html; charset=gb2312">  </head>
      
      <body>
        <form action="rets" method="post">
       用户id:<input type="text" size=20 name="username">
       <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="submit" value="提交">
        </form>
      </body>
    </html>而处理的代码是这个:package xt;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 rets extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter(); String username = request.getParameter("username").trim();
    out
    .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>结果</TITLE></HEAD>");
    out.println("  <BODY>"); if (username.equals("")) {
    out.print("Login failed!");
    } else {
    out.print("Login succeeded!");
    }
    out.println("<br>"+username+"<br>");
    out
    .println("<a href=http://10.10.21.187:8080/ServerTest/index.jsp>返回</a>");
    out.println("  </BODY>");
    out.println("</HTML>"); out.flush();
    out.close();
    } public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request, response);
    }}
    用的java severlet+tomcat
      

  5.   

    亲  你的这个代码 text 值似乎传不进去啊 ,在toast里面提示出错!
      

  6.   


    你的方法可以正确的得到返回值了  但是返回的只是我那个网页处理的源代码的,并没有返回处理后的数据啊……
    返回的是这个<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>    
        <title>用户登录界面</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <meta http-equiv="content-type" content="text/html; charset=gb2312">  </head>
      
      <body>
        <form action="rets" method="post">
       用户id:<input type="text" size=20 name="username">
       <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="submit" value="提交">
        </form>
      </body>
    </html>而处理的代码是这个:package xt;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 rets extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter(); String username = request.getParameter("username").trim();
    out
    .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>结果</TITLE></HEAD>");
    out.println("  <BODY>"); if (username.equals("")) {
    out.print("Login failed!");
    } else {
    out.print("Login succeeded!");
    }
    out.println("<br>"+username+"<br>");
    out
    .println("<a href=http://10.10.21.187:8080/ServerTest/index.jsp>返回</a>");
    out.println("  </BODY>");
    out.println("</HTML>"); out.flush();
    out.close();
    } public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request, response);
    }}
    用的java severlet+tomcat
    我的示例代码用的是你实现的函数submitPostData,如果返回的数据不对,说明是你函数实现的问题。
    你检查一下是不是url写错了。
      

  7.   

    package com.example.DialogForStart;import org.json.JSONException;
    import org.json.JSONObject;import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import conn.ReadTextHttpClient;
    import entity.UpdateInfo;public class MainActivity extends Activity { @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    checkUpdate();
    } private void checkUpdate() {
    new Thread(new Runnable() {

    @Override
    public void run() {
    ReadTextHttpClient rt=new ReadTextHttpClient("http://10.0.2.2:8080/DialogForStart/DialogServlet");
    String text=rt.doPost();
    UpdateInfo uInfo=getUpdateFromJson(text);
    Message message=new Message();
    if (uInfo!=null) {
    message.obj=uInfo;
    message.what=1;
    }else {
    message.what=0;
    }

    myHandler.sendMessage(message);
    }
    }).start();
    } Handler myHandler = new Handler(){
     
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {         
                case  0:             break;
                case  1:
                 final UpdateInfo uInfo=(UpdateInfo) msg.obj;
                 AlertDialog.Builder builder = new Builder(MainActivity.this);
                
                 builder.setTitle(uInfo.getTitle());
                    builder.setMessage(uInfo.getMsg());
                    //设置点击屏幕Dialog不消失 
                    builder.setCancelable(false);
                    builder.setPositiveButton("确认",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                         //点击确定,调用系统默认浏览器下载指定url的apk
                         Intent intent = new Intent();        
                         intent.setAction("android.intent.action.VIEW");    
                         Uri content_url = Uri.parse(uInfo.getUrl());   
                         intent.setData(content_url);  
                         startActivity(intent);
                        }
                    });
                    if (!uInfo.isMust()) {
                     builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //dialog.dismiss();
                            }
                        });
    }
                  builder.create().show();
                    break;
                }
            }
             
        };
        /**
     * 解析json
     * @param text
     * @return
     */
    private UpdateInfo getUpdateFromJson(String text) {
    UpdateInfo uInfo=new UpdateInfo();
    try {
    JSONObject jsonObject = new JSONObject(text);
    //获取大对象中up对象json
    JSONObject jsonuInfo = jsonObject.getJSONObject("up");
    uInfo.setTitle(jsonuInfo.getString("title"));
    uInfo.setMsg(jsonuInfo.getString("msg"));
    uInfo.setMust(jsonuInfo.getBoolean("must"));
    uInfo.setUrl(jsonuInfo.getString("url"));
    } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
    }
    return uInfo;
    }}
    这几天没空来csdn,这是我之前写的一个demo
      

  8.   

    private void myRunOnUiThread(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                DatiActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toResult();
                        pd2.dismiss();
                    }
                });
            }
        }).start();
    }
    这是最后一招了