Android排球图书馆异常处理

时间:2016-07-24 16:11:12

标签: java android exception-handling android-volley

我递归地运行以下代码检查到我的数据库,直到字段变为1.问题是当我想同时运行多个函数时,我需要处理异常。 这是一个简单的例子:

package com.example.sakis.responseproject;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

TextView text1;
TextView text2;
TextView text3;
int back1;
int back2;
int back3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   func1();
   func2();
    func3();
}




public void func1()  {


    text1=(TextView) findViewById(R.id.Value1);

    Response.Listener<String> response1Listener = new Response.Listener<String>() {
        @Override
        public void onResponse(final String response) {


            try {

                JSONObject jsonResponse = new JSONObject(response);
                back1 = jsonResponse.getInt("back");

                if (back1 == 0) {
                    text1.setText(" " + back1);
                    func1();
                } else {
                    text1.setText(" " + back1);
                }


            } catch (JSONException e)

            {

                Log.w("Catch1", "Catch1");
                e.printStackTrace();
                ///      func1();
            }


        }





        };


    ResponseRequest backRequestRequest = new ResponseRequest(response1Listener);
    RequestQueue queue1 = Volley.newRequestQueue(MainActivity.this);
    queue1.add(backRequestRequest);









}


public void func2()  {

    text2=(TextView) findViewById(R.id.Value2);


    Response.Listener<String> response1Listener = new Response.Listener<String>() {
        @Override
        public void onResponse(final String response) {


            try {

                JSONObject jsonResponse = new JSONObject(response);


                back2=jsonResponse.getInt("back");

                if(back2==0)  {
                    text2.setText(" "+back2);
                    func2();
                }
                else {
                    text2.setText(" "+back2);
                }

            } catch (JSONException e)

            {
                Log.w("Catch2","Catch2");
                e.printStackTrace();
           ///     func2();
            }


        }


    };


    ResponseRequest backRequestRequest = new ResponseRequest(response1Listener);
    RequestQueue queue1 = Volley.newRequestQueue(MainActivity.this);
    queue1.add(backRequestRequest);






}



public void func3()  {

    text3=(TextView) findViewById(R.id.Value3);



    Response.Listener<String> response1Listener = new Response.Listener<String>() {
        @Override
        public void onResponse(final String response) {


            try {

                JSONObject jsonResponse = new JSONObject(response);

                back3=jsonResponse.getInt("back");

                if(back3==0)  {
                    text3.setText(" "+back3);
                    func3();
                }
                else {
                    text3.setText(" "+back3);
                }


            } catch (JSONException e)

            {
                Log.w("Catch3","Catch3");
                e.printStackTrace();
              //  func3();
            }


        }


    };


    ResponseRequest backRequestRequest = new ResponseRequest(response1Listener);
    RequestQueue queue1 = Volley.newRequestQueue(MainActivity.this);
    queue1.add(backRequestRequest);






}



}

这是ResponseRequest.class:

package com.example.sakis.responseproject;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

public class ResponseRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL ="http://xxxx/Response.php";
private Map<String, String> params;

public ResponseRequest( Response.Listener<String> listener) {
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);
    params = new HashMap<>();


}

@Override
public Map<String, String> getParams() {
    return params;
}
}

还有Response.php:

<?php

 $connect = mysqli_connect("xxx", "xxx",     "xxx", "xxx");

 $username1 ="elpi";


 $sql = "SELECT back FROM User WHERE username='$username1'";

$result = mysqli_query($connect, $sql);

$row = mysqli_fetch_assoc($result);


$response["back"]=$row[back];

print_r(json_encode($response));

?>

一个函数正常工作但是当我需要递归检查更多字段时,应用程序每次都在不同的时间崩溃,我想处理这些异常。我需要尝试再次运行递归函数,当它因某些原因失败时

0 个答案:

没有答案