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

时间:2013-06-19 20:44:15

标签: php android http-post

我正在研究如何通过AndroidMySQLPHP联系起来的教程。我编辑了本教程中的代码,但无法发送POSTGET数据。 我总是有消息Login and/or password field empty

这是我的方法:

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();
        }
    }
}

这是我的JSON解析器:

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+"\n");
        }
        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;
}
}

这是我的PHP脚本:

$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 个答案:

没有答案