我是技术新手。所以我坚持使用webservices。我创建了简单的登录表单。这是我的Mainactivity类,其中我已经完成了简单的webservice连接解析数据throgh json对象。我只想从我的网络服务登录我的用户名和密码,但我无法这样做。我也给了网络权限。任何人都可以告诉我我的错误。提出一些解决方案。
package com.example.shrutir.login;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by shruti.r on 3/5/2016.
*/
public class Test_login extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
// new AsyncTaskParseJson().execute();
Button sign = (Button) findViewById(R.id.sign_in);
final EditText usr = (EditText) findViewById(R.id.usrname_dr);
final EditText passw = (EditText) findViewById(R.id.pswrd_dr);
sign.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AsyncTaskParseJson().execute();
}
});
}
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = "AsyncTaskParseJson.java";
EditText usr = (EditText) findViewById(R.id.usrname_dr);
EditText passw = (EditText) findViewById(R.id.pswrd_dr);
String yourJsonStringUrl ="Here is my json webservice url";
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {
}
protected String doInBackground(String... arg0) {
try {
// instantiate our json parser
JSONParser jParser = new JSONParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray("GetUserLoginResult");
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
// Storing each json item in variable
String name = c.getString("UserName");
// String lastname = c.getString("LastName");
String password = c.getString("Password");
// show the values in our logcat
Log.e(TAG, "firstname: " + name + ", lastname: " + ",
password:" + password);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg) {
if (usr.equals("name")&& passw.equals("password")){
Intent m = new Intent(Test_login.this,Newpage.class);
startActivity(m);
Toast.makeText(getApplicationContext(),"Login
Sucessful",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), "Login failed",
Toast.LENGTH_SHORT).show();
}
}
}
}
这是我的xml文件:这里我创建了一个简单的用户名和密码登录表单
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f1f1f1"
android:weightSum="1">
<!-- android:background="@drawable/loginbg"-->
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="458dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="40dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="30dp"
android:orientation="vertical"
android:gravity="center_horizontal">
<EditText
android:id="@+id/usrname_dr"
android:textSize="15dp"
android:layout_width="265dp"
android:layout_height="40dp"
android:hint=" Username"
android:background="#FCFCFC"
android:textColorHint="#666666"
android:paddingLeft="5dp"
android:textColor="#10c1b9"
android:layout_marginTop="40dp"/>
<EditText
android:textSize="15dp"
android:id="@+id/pswrd_dr"
android:layout_width="265dp"
android:layout_height="40dp"
android:hint=" Password"
android:inputType="textPassword"
android:textColor="#10c1b9"
android:background="#FCFCFC"
android:textColorHint="#666666"
android:paddingLeft="5dp"
android:layout_marginTop="20dp"/>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<TextView
android:layout_marginRight="10dp"
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/frgtpaaswrd"
android:textColor="#415052"
android:text="Forget Password?"/>
<CheckBox
android:id="@+id/forgot_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_marginLeft="10dp"
android:text="Remember Me "
android:textColor="#415052"
/>
</LinearLayout>
<Button
android:id="@+id/sign_in"
android:layout_width="265dp"
android:layout_height="38dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="Sign In"
android:textColor="#ffff"
android:textSize="13dp"
android:onClick="sign_in"
android:background="#10c1b9"/>
<Button
android:id="@+id/sign_up"
android:layout_width="265dp"
android:layout_height="38dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Sign Up"
android:textColor="#ffff"
android:textSize="14dp"
android:background="#415052"/>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
变量名称和密码应该是公开的
你的错误
usr.equals("name")&& passw.equals("password")
它应该
usr.equals(name)&& passw.equals(password) // variable not string