dongyue8640 2013-06-19 20:44
浏览 30

无法通过setEntity发送POST或GET数据

I working on a tutorial how to connect Android to MySQL via PHP. I edited my code from this tutorial and I can't send POST or GET data. I always have message Login and/or password field empty

This is my method:

class LogIn extends AsyncTask<String, String, String>{

    String state, msg;
    String id, login, pass;
    protected void onPreExecute(){
        super.onPreExecute();
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Checking, please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        List<NameValuePair> parames = new ArrayList<NameValuePair>(1);
        parames.add(new BasicNameValuePair("login", u_name.getText().toString()));
        parames.add(new BasicNameValuePair("pass", u_pass.getText().toString()));

        Log.e("[INFO]",u_name.getText().toString());
        Log.e("[INFO]",u_pass.getText().toString());
        JSONObject json = jParser.makeHttpRequest(check_url, "POST", parames);

        try{
            int success = json.getInt("success");
            if(success == 1){
                SharedPreferences.Editor edytor = preferences.edit();
                edytor.putString("name", json.getString("name"));
                edytor.putString("login", u_name.toString());
                edytor.putString("pass", u_pass.toString());
                edytor.putBoolean("logged", true); //dfdf
                edytor.commit();
                state = "ok";
            }
            else{
                SharedPreferences.Editor edytor = preferences.edit();
                edytor.putBoolean("logged", false);
                msg = json.getString("message");
                state = "not_ok";

            }
        } catch(JSONException e){
            Log.e("[JSON ERROR]: ", e.toString());
        }
        return null;
    }

    protected void onPostExecute(String file_url){
        pDialog.dismiss();
        if(state.equals("ok")){
            Toast.makeText(LoginActivity.this, "You are logged in ;)", Toast.LENGTH_LONG).show();
            Intent i = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(i);
        }else if(state.equals("not_ok")){
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_LONG).show();
        }
    }
}

and this is my JSON parser:

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params){
    try{
        if(method == "POST"){
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            Log.e("[TEST]",params.toString());
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }
        else if(method == "GET"){
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "iso-8859-1");
            url+="?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }
    } catch(UnsupportedEncodingException e){
        e.printStackTrace();
    } catch(ClientProtocolException e){
        e.printStackTrace();
    } catch(IOException e){
        e.printStackTrace();
    }

    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) != null){
            sb.append(line+"
");
        }
        is.close();
        json = sb.toString();
    } catch(Exception e){
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    try{
        jObj = new JSONObject(json);
    } catch(JSONException e){
        Log.e("JSON Parser", "Error parsing data " + e.toString() + " DATA: "+json);
    }

    return jObj;
}
}

and this is my PHP script:

$response = array();
if( (!empty($_POST['login'])) && (!empty($_POST['pass'])) )
{
    include('config.php');
$zapytanie = mysql_query("SELECT * FROM users WHERE login = '".$_POST['login']."' AND pass='".$_POST['pass']."' LIMIT 1");
    if(mysql_num_rows($zapytanie)>0){
        while($row = mysql_fetch_assoc($zapytanie)){
            $response["name"] = $row["name"];
            $response["success"] = 1;
        }
    }
    else{
        $response["success"] = 0;
        $response["message"] = "Bad login or pass";
    }
} else{
$response["success"] = 0;
$response["message"] = "Login and/or password field empty";
}
print(json_encode($response));
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度