[英]My jasonParser class in doesnot get response from php web Service
Code: 码:
Login Class 登录类别
public class Login extends Activity implements View.OnClickListener {
ImageButton bLogin;
EditText UserName;
EditText PassWord;
TextView Msg;
String email, password;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "Example.com";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
UserName = (EditText) findViewById(R.id.etUserName);
PassWord = (EditText) findViewById(R.id.etPassword);
bLogin = (ImageButton) findViewById(R.id.bLogin);
Msg = (TextView) findViewById(R.id.tvMsg);
bLogin.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bLogin:
new AttemptLogin().execute();
break;
default:
break;
}
}
AttemptLogin 尝试登录
class AttemptLogin extends AsyncTask<String, String, String> {
//three methods get called, first preExecture, then do in background, and once do
//in back ground is completed, the onPost execture method will be called.
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
int success;
String username = UserName.getText().toString();
String password = PassWord.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Username", username));
params.add(new BasicNameValuePair("Password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// Toast.makeText(Login.this,
//(int) msg, Toast.LENGTH_LONG).show();
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == -1) {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(Login.this, com.Office.bilawalkhan.projectmanagment.Menu.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
JSOnParser Class JSOnParser类
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
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, "iso-8859-1"), 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 parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
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, "utf-8");
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, "iso-8859-1"), 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 parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
// constructor
}
You can refer http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ 您可以参考http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
Or 要么
You can use "Volley" library by google for faster and better networking . 您可以使用Google的“ Volley”库来实现更快更好的联网。 This library nicely plays with Json . 这个库很好地与Json一起玩。 This library makes easy to cancel request for url which is not possible in AsyncTask http://bon-app-etit.blogspot.in/2013/04/the-dark-side-of-asynctask.html . 通过该库,可以轻松取消AsyncTask http://bon-app-etit.blogspot.in/2013/04/the-dark-side-of-asynctask.html中不可能的url请求。
Here are some examples of using Volley : http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ 以下是一些使用Volley的示例: http : //www.androidhive.info/2014/05/android-working-with-volley-library-1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.