如果mysql数据库中没有可用数据,应用程序崩溃

时间:2016-06-30 09:20:08

标签: php android android-asynctask

当Serialno和city在数据库中不可用时,我的应用程序崩溃,有时显示“没有数据可用”,有时它会崩溃,我需要的是显示吐司,当数据库中没有序列号和城市时

 public String getvalues()
   {
    String uri = "http://www.lorryguru.com/retusno.php?city="+Citys+"&serialno="+SerialNo;
    HttpGet httpGet = new HttpGet(uri);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1)
        {
            stringBuilder.append((char) b);
        }
    }
    catch (ClientProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    try
    {
        JSONArray arr = new JSONArray(stringBuilder.toString());
       //result = jsonObj.getJSONArray(TAG_RESULT);
        JSONObject c = arr.getJSONObject(0);
        id = c.getString(TAG_ID);
        district = c.getString(TAG_DISTRICT);
        cit = c.getString(TAG_CITY);
        serial_no = c.getString(TAG_SERIALNO);
        gende = c.getString(TAG_GENDER);
        wareno = c.getString(TAG_WARENO);
        befor = c.getString(TAG_BEFORE);
        afte = c.getString(TAG_AFTER);
        latitude = c.getString(TAG_LATITUDE);
        longitude = c.getString(TAG_LONGITUDE);
        return "success";
    }
    catch (JSONException e)
    {
        return "No Data is Available";
       //e.printStackTrace();
    }
}

我的php脚本是

       <?php
        require "init.php";

       $serialno = $_REQUEST["serialno"];
       $city = $_REQUEST["city"];

       $sql = "SELECT * FROM dogs_details WHERE serial_no='".$serialno."' &&city='".$city."' ";
      $res = $con->query($sql);
      $result = array();

      while($row = mysqli_fetch_array($res)){

     if($row['after_dog']=="")
     {
     $afterimage="No Image";    
      }
    else
     {
    $afterimage=$row['after_dog'];  
     }

    array_push($result,
    array('id'=>$row['id'],
    'district'=>$row['district'],
     'city'=>$row['city'],
    'serialno'=>$row['serial_no'],
    'gender'=>$row['gender'],
    'wareno'=>$row['ware_no'],
    'before'=>$row['before_dog'],
    'after'=>$afterimage,
    'latitude'=>$row['latitude'],
    'longitude'=>$row['longitude']
     ));
     }
    echo json_encode($result);

    mysqli_close($con);
    ?>

我的postExecute位于

之下
            protected void onPostExecute(String s) {

            InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
            dialog.hide();
            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
            dis.setText("District   " + district);
            city.setText("City      " + cit);
            serialno.setText("Serial No.  " + serial_no);
            gender.setText("Gender   " + gende);
            Picasso.with(Serialno.this).load(befor).into(before);
            if (afte.equals("No Image")) {
                after.setImageDrawable(getResources().getDrawable(R.drawable.load));
            } else {
                Picasso.with(Serialno.this).load(afte).into(after);
            }

            // loction.setText("Latitude   "+latitude+"  "+"Longitude   "+longitude);
            Showing_Map();

        }

1 个答案:

答案 0 :(得分:1)

检查JsonObject的所有字段的无效性,然后尝试使用它们。例如:

try
{
    JSONArray arr = new JSONArray(stringBuilder.toString());
   //result = jsonObj.getJSONArray(TAG_RESULT);
    JSONObject c = arr.getJSONObject(0);

    if(c.isNull(TAG_ID)
       id="";
    else
       id = c.getString(TAG_ID);

    if(c.isNull(TAG_DISTRICT)
       district="";
    else
       district = c.getString(TAG_DISTRICT);

    ..........................
   ...............................

    return "success";
}
catch (JSONException e)
{
    return "No Data is Available";
   //e.printStackTrace();
}

在onPostExecute中显示Toast:

protected void onPostExecute(String s) {

        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
        dialog.hide();
        Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
        dis.setText("District   " + district);

  //Toast when city is null
    if(cit.equals(""))
       Toast.makeText(getApplicationContext(), YOUR_MESSSAGE, Toast.LENGTH_SHORT).show();

  //Toast when serail_no is null
  if(serail_no .equals(""))
       Toast.makeText(getApplicationContext(), YOUR_MESSSAGE, Toast.LENGTH_SHORT).show();


        city.setText("City      " + cit);
        serialno.setText("Serial No.  " + serial_no);
        gender.setText("Gender   " + gende);
        Picasso.with(Serialno.this).load(befor).into(before);
        if (afte.equals("No Image")) {
            after.setImageDrawable(getResources().getDrawable(R.drawable.load));
        } else {
            Picasso.with(Serialno.this).load(afte).into(after);
        }

        // loction.setText("Latitude   "+latitude+"  "+"Longitude   "+longitude);
        Showing_Map();

    }