Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android studio离线登录注册_Java_Android_Json_Eclipse_Import - Fatal编程技术网

Java Android studio离线登录注册

Java Android studio离线登录注册,java,android,json,eclipse,import,Java,Android,Json,Eclipse,Import,当我离线登录时,我的应用程序正在崩溃…并显示错误 尝试在空对象引用上调用虚拟方法“java.lang.String org.json.JSONObject.getString(java.lang.String)” 在网上它很好,没有问题,但在线下它崩溃了,根本没有给出问题所在,请帮我解决这个问题 public class MainActivity extends AppCompatActivity { **// Initializing variables**

当我离线登录时,我的应用程序正在崩溃…并显示错误
尝试在空对象引用上调用虚拟方法“java.lang.String org.json.JSONObject.getString(java.lang.String)”

在网上它很好,没有问题,但在线下它崩溃了,根本没有给出问题所在,请帮我解决这个问题

public class MainActivity extends AppCompatActivity {

            **// Initializing variables**
            EditText login;
            EditText password;

            String statusRes;
            String id;
            String projectName;
            String loginValue;
            String stockPoint;
            JSONObject myRespObject = null;

            public static final String Passkey = "passKey";

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setTitle("LOGIN");
                setContentView(R.layout.login);
                login = (EditText) findViewById(R.id.loginname);
                password = (EditText) findViewById(R.id.Password);    
                final Button saveme = (Button) findViewById(R.id.save);       
                **SharedPreferences sharedpreferences = getSharedPreferences(AppConstants.MyPREFERENCES, Context.MODE_PRIVATE);
                saveme.setOnClickListener(new Button.OnClickListener() {
                   public URL url;
                    public void onClick(android.view.View v) {
                       if (!CheckNetwork.isInternetAvailable(MainActivity.this){
                            if (!validate()) {
                                onLoginFailed();
                                return;
                            }
SharedPreferences prefs = getSharedPreferences(AppConstants.MyPREFERENCES, Context.MODE_PRIVATE);
 String loginValue = prefs.getString(AppConstants.LOGIN_VALUE, "");
String Passkey = prefs.getString(AppConstants.PASS_KEY, "");
String Internet = prefs.getString("Internet", "false");
String projectName = prefs.getString(AppConstants.PROJECT_NAME, "");
String stockPoint = prefs.getString(String.valueOf(AppConstants.STOCK_POINT),"");
String id = prefs.getString(AppConstants.ID, "");
 Intent profactivity = new Intent(MainActivity.this, View.class);
                                profactivity.putExtra("Internet", false);
                                profactivity.putExtra("loginValue", loginValue);
                                profactivity.putExtra("id", id);
               profactivity.putExtra("projectName", projectName);
                                profactivity.putExtra("stockPoint", stockPoint);
                                startActivity(profactivity);
**Toast.makeText(MainActivity.this, "Offline Login  ", Toast.LENGTH_SHORT).show();
                                finish();
                            }
****对于上面的代码,这里是通过错误**

       try {
final String loginValue = URLEncoder.encode(login.getText().toString(), "UTF-8");
final String passValue = URLEncoder.encode(password.getText().toString(), "UTF-8");
  try {
        new Thread(new Runnable() {
                  **//Thread to stop network calls on the UI thread**
                                    public void run() {
                                        //Request the HTML
                                        ArrayList<String> list = null;
                                        try {
String loginValue = URLEncoder.encode(login.getText().toString(), "UTF-8");
String passValue = URLEncoder.encode(password.getText().toString(), "UTF-8");
 String ROOT_URL = getResources().getString(R.string.ROOT_URL) + "/api/v1/user/signIn?loginName=" + loginValue + "&password=" + passValue;
  Log.i("httpget", "#####@@@@@@#####" + ROOT_URL);
        HttpClient client = new DefaultHttpClient();
       HttpGet request = new HttpGet(ROOT_URL);
         HttpResponse response = client.execute(request);
     if (response.getStatusLine().getStatusCode() == 200) {
   String server_response = EntityUtils.toString(response.getEntity());
    myRespObject = new JSONObject(server_response);
         //Do something with the response
  //Toast.makeText(getBaseContext(),server_response,Toast.LENGTH_LONG).show();
    statusRes = myRespObject.getString("status");
               JSONObject respObject = myRespObject.getJSONObject("response");
          id = respObject.getString("_id");
     AppConstants._ID = id;
     projectName = respObject.getString("projectName");
            Actors actor = new Actors();
               list = new ArrayList<>();
             JSONArray jsonArray = respObject.getJSONArray("stockPoint");
     Intent i = getIntent();
       Serializable subject = i.getSerializableExtra("stockPoint");
        if (jsonArray != null) {
     int len = jsonArray.length();
            for (int k = 0; k < len; k++)
                 list.add(jsonArray.get(k).toString());
                                                }
            actor.setStockPoint(list);
     AppConstants.STOCK_POINT = stockPoint;
      stockPoint = respObject.getString("stockPoint");
                                            }
       } catch (UnsupportedEncodingException e) {
                                            e.printStackTrace();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                       final ArrayList<String> finalList = list;
                                        runOnUiThread(new Runnable() {

            @Override
        public void run() {
           try {
            statusRes = myRespObject.getString("status");
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
    if (statusRes.equalsIgnoreCase("success")) {     
   SharedPreferences sharedpreferences = getSharedPreferences(AppConstants.MyPREFERENCES, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpreferences.edit();
             editor.putString(AppConstants.LOGIN_VALUE, loginValue);
      editor.putString(AppConstants.PASS_KEY, passValue);
      editor.putString("Internet", "true");
        editor.putString(AppConstants.ID, id);
   editor.putString(AppConstants.PROJECT_NAME, projectName);
   editor.putString(String.valueOf(AppConstants.STOCK_POINT), String.valueOf(stockPoint));
     editor.commit();
    **//Here move to next screen or home screen**
   Intent profactivity = new Intent(MainActivity.this, View.class);                                                profactivity.putExtra("Internet", true);                                                   profactivity.putExtra("loginValue", loginValue);                                                  profactivity.putExtra("id", id);
                                                    profactivity.putExtra("projectName", projectName);
                                                    profactivity.putExtra("stockPoint", finalList);
   startActivity(profactivity);
                                                    Toast.makeText(MainActivity.this, "Login Successfully", Toast.LENGTH_LONG).show();
     finish();
   } else if (statusRes.equalsIgnoreCase("failed")) {
          if (!validate()) {
          onLoginFailed();
         return;
         }
         }
      }
 });
  }

}).start();

  //return data;
 } catch (Exception e) {
 Log.i("httpget", "#####@@@@@@#####Error1 -->" + e.getStackTrace());
                                **Toast.makeText(getBaseContext(), "ERROR : " + e.getMessage(), Toast.LENGTH_LONG).show();**
                            }

                        } catch (UnsupportedEncodingException ex) {

                            finish();
                        }
                    }
                });
            }
            public boolean validate() {
                boolean valid = true;

                String email = login.getText().toString();
                String passwor = password.getText().toString();

                if (email.isEmpty() || email.length() < 2 || email.length() > 10) {
                    login.setError("enter valid username");
                    valid = false;
                } else {
                    login.setError("Invalid username");
                }

  if (passwor.isEmpty() || passwor.length() < 2 || passwor.length() > 10) {
                    password.setError("enter valid password");
                    valid = false;
                } else {
                    password.setError("Invalid password");
                }

                return valid;
            }
            public void onLoginFailed() {
                **Toast.makeText(getBaseContext(), "Invalid login", Toast.LENGTH_LONG).show();**

            }
              }
--------------------------------------------------------------------------------
试试看{
最终字符串loginValue=URLEncoder.encode(login.getText().toString(),“UTF-8”);
最终字符串passValue=URLEncoder.encode(password.getText().toString(),“UTF-8”);
试一试{
新线程(newrunnable()){
**//线程停止UI线程上的网络调用**
公开募捐{
//请求HTML
ArrayList list=null;
试一试{
String loginValue=URLEncoder.encode(login.getText().toString(),“UTF-8”);
字符串passValue=URLEncoder.encode(password.getText().toString(),“UTF-8”);
String ROOT_URL=getResources().getString(R.String.ROOT_URL)+“/api/v1/user/sign?loginName=“+loginValue+”&password=“+passValue;
Log.i(“httpget”、“根URL”);
HttpClient=new DefaultHttpClient();
HttpGet请求=新的HttpGet(根URL);
HttpResponse response=client.execute(请求);
if(response.getStatusLine().getStatusCode()==200){
String server_response=EntityUtils.toString(response.getEntity());
myRespObject=新的JSONObject(服务器响应);
//对回应做点什么
//Toast.makeText(getBaseContext(),server_response,Toast.LENGTH_LONG.show();
statusRes=myRespObject.getString(“状态”);
JSONObject respObject=myRespObject.getJSONObject(“响应”);
id=respObject.getString(“\u id”);
AppConstants.\u ID=ID;
projectName=respObject.getString(“projectName”);
演员=新演员();
列表=新的ArrayList();
JSONArray JSONArray=respObject.getJSONArray(“stockPoint”);
Intent i=getIntent();
Serializable subject=i.getSerializableExtra(“stockPoint”);
if(jsonArray!=null){
int len=jsonArray.length();
对于(int k=0;k10){
login.setError(“输入有效用户名”);