【发布时间】:2014-01-02 11:39:47
【问题描述】:
我正在开发一个应用程序,我必须使用用户名和密码登录,以便无效用户无法登录,而有效用户可以。用户无法注册,将获得唯一的ID和密码。
因此,我为此使用了 JSON POST 方法。
但我无法发布数据。无论我提供什么 ID 和密码,都会收到默认错误消息。
错误消息:
01-02 11:31:38.047: V/响应 (2888): {"Login":"false","error":{"email":"请输入有效的Email 地址。","密码":"请输入您的密码。"}}
完整的代码贴在下面。
TextView tvIsConnected;
EditText username;
EditText password;
Button login;
Driver driver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
// check if you are connected or not
if(isConnected()){
tvIsConnected.setBackgroundColor(0xFF00CC00);
tvIsConnected.setText("You are conncted");
}
else{
tvIsConnected.setText("You are NOT conncted");
}
// add click listener to Button "POST"
login.setOnClickListener(this);
}
public static String POST(String url, Driver driver){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("username", driver.getUsername());
jsonObject.accumulate("password", driver.getPassword());
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
// TODO Auto-generated method stub
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
private boolean isConnected() {
// TODO Auto-generated method stub
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch(view.getId()){
case R.id.login:
if(!validate())
Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show();
// call AsynTask to perform network operation on separate thread
new HttpAsyncTask().execute("http://dfl.tulieservices.biz/webservices/userOperation/user_login");
break;
}
}
public class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
driver = new Driver();
driver.setUsername(username.getText().toString());
driver.setPassword(password.getText().toString());
Log.v("USERNAME", username.getText().toString());
Log.v("PASSWORD", password.getText().toString());
return POST(urls[0],driver);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Log.v("Response", result);
Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
}
}
private boolean validate(){
if(username.getText().toString().trim().equals(""))
return false;
else if(password.getText().toString().trim().equals(""))
return false;
else
return true;
}
}
【问题讨论】:
-
id未注册或不存在
-
到底是什么问题?正如您所展示的,您已经收到了来自服务器的响应。
-
我收到来自服务器的响应,这是默认错误消息,以防 id 和密码错误。问题是当我提供有效的 ID 和密码@PareshMayani 时,会出现相同的错误消息@PareshMayani
-
如果您确定您发送了正确的 id 和密码,那么问题一定出在服务器端,服务器代码是相关的,而不是 Android 代码。在服务器端调试和 1. 确保您收到正确的 id/密码 2. 看看为什么它仍然不起作用
标签: java android json web-services getjson