2017-04-01 97 views
0

当我从index.php中删除HTML代码并保持php之间的连接时,我的代码正常工作。但是,当我使用HTML线(我需要其他部分的应用程序)显示时,它会给出错误:与HTML一起使用Retrofit时出现JSON错误

“使用JsonReader.setLenient(true)在第1行接受格式错误的JSON 1 path 1 $ “

我曾尝试使用

Gson gson = new GsonBuilder() 
      .setLenient() 
      .create(); 

Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(Constants.BASE_URL) 
      .addConverterFactory(GsonConverterFactory.create(gson)) 
      .build(); 

但它给了我另一个错误:

”java.lang.IllegalStateException:预期BEGIN_OBJECT但STRING在第1分1条路径$“

这是我的Logcat output

如何解决?!!!

FirstPOIPreference.java:

public class FirstPOIPreference extends DialogPreference { 

private EditText et_alias, et_keyword, et_address; 
private Spinner sp_city; 
private ArrayAdapter<String> adapter; 
private boolean deleted; 
private Context context; 
private SharedPreferences pref; 
private POI poiDialog = new POI(); 

public FirstPOIPreference(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.context = context; 
    init(); 
} 

private void init() { 
    setPersistent(false); 
    setDialogLayoutResource(R.layout.pref_poi_dialog); 
    setPositiveButtonText(android.R.string.ok); 
    setNegativeButtonText(android.R.string.cancel); 
} 

@Override 
protected void onBindDialogView(View view) { 
    super.onBindDialogView(view); 
    pref = PreferenceManager.getDefaultSharedPreferences(this.context); 
    load(getSharedPreferences()); 
    initViews(view); 
    fillViews(); 
} 

private void load(SharedPreferences prefs) { 
    poiDialog.setAlias(prefs.getString(Constants.FIRST_POI_ALIAS, "")); 
    poiDialog.setKeyword(prefs.getString(Constants.FIRST_POI_KEYWORD, "")); 
    poiDialog.setAddress(prefs.getString(Constants.FIRST_POI_ADDRESS, "")); 
    poiDialog.setCity((prefs.getString(Constants.FIRST_POI_CITY, ""))); 
} 

private void initViews(View view) { 
    deleted = false; 
    TextView tv_delete = (TextView) view.findViewById(R.id.tv_poi_delete); 
    et_alias = (EditText) view.findViewById(R.id.et_poi_alias); 
    et_keyword = (EditText) view.findViewById(R.id.et_poi_keyword); 
    et_address = (EditText) view.findViewById(R.id.et_poi_address); 
    sp_city = (Spinner) view.findViewById(R.id.spinner_poi_city); 
    sp_city.setPrompt("City"); 
    String[] arrayCity = new String[]{"Erie", "Pittsburgh", "Cleveland", "Buffalo", "Niagara Falls", "Jamestown"}; 
    adapter = new ArrayAdapter <> (this.getContext(), 
      android.R.layout.simple_spinner_dropdown_item, arrayCity); 
    sp_city.setAdapter(adapter); 

    et_alias.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
      poiDialog.setAlias(et_alias.getText().toString()); 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
     } 
    }); 

    et_keyword.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
      poiDialog.setKeyword(et_keyword.getText().toString()); 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
     } 
    }); 

    et_address.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
      poiDialog.setAddress(et_address.getText().toString()); 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
     } 
    }); 

    sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView <?> parentView, View selectedItemView, 
            int position, long id) { 
      poiDialog.setCity(sp_city.getSelectedItem().toString()); 
     } 

     @Override 
     public void onNothingSelected(AdapterView <?> parentView) { 
     } 
    }); 

    tv_delete.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      AlertDialog.Builder delete_builder = new AlertDialog.Builder(context); 
      delete_builder.setMessage("Are you sure you want to delete this point of interest?") 
        .setCancelable(false) 
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          Toast.makeText(context, "Deleting POI !", Toast.LENGTH_LONG).show(); 
          deletePOI(); 
         } 
        }) 
        .setNegativeButton("No", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alertDelete = delete_builder.create(); 
      alertDelete.setTitle("Delete POI!"); 
      alertDelete.show(); 
     } 
    }); 
} 

private void fillViews() { 
    et_alias.setText(poiDialog.getAlias()); 
    et_keyword.setText(poiDialog.getKeyword()); 
    et_address.setText(poiDialog.getAddress()); 
    sp_city.setSelection(adapter.getPosition(poiDialog.getCity())); 
} 

@Override 
protected void onDialogClosed(boolean positiveResult) { 
    if (positiveResult) { 
     String alias, keyword, address, city; 
     if(deleted) { 
      et_alias.setText(""); 
      et_keyword.setText(""); 
      et_address.setText(""); 
      sp_city.setPrompt("City"); 
     } else { 
      alias = et_alias.getText().toString(); 
      keyword = et_keyword.getText().toString(); 
      address = et_address.getText().toString(); 
      city = sp_city.getSelectedItem().toString(); 
      if (!keyword.isEmpty() && !address.isEmpty() && !city.isEmpty()) { 
       Toast.makeText(context, "Saving POI !", Toast.LENGTH_LONG).show(); 
       addPOI(alias, keyword, address, city); 
      } else { 
       Toast.makeText(context, "Required Fields are empty !", Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 
} 

private void addPOI(String alias, String keyword, String address, String city) { 
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
    OkHttpClient client = new OkHttpClient.Builder() 
      .addInterceptor(interceptor) 
      .retryOnConnectionFailure(true) 
      .connectTimeout(15, TimeUnit.SECONDS) 
      .build(); 

    Gson gson = new GsonBuilder() 
      .setLenient() 
      .create(); 

    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(Constants.BASE_URL) 
      .client(client) 
      .addConverterFactory(GsonConverterFactory.create(gson)) 
      .build(); 

    RequestInterface requestInterface = retrofit.create(RequestInterface.class); 

    User user = new User(); 
    user.setEmail(pref.getString(Constants.USER_EMAIL, "")); 

    POI poi = new POI(); 
    poi.setAlias(alias); 
    poi.setKeyword(keyword); 
    poi.setAddress(address); 
    poi.setCity(city); 

    ServerRequest request = new ServerRequest(); 
    request.setOperation(Constants.ADD_POI); 
    request.setUser(user); 
    request.setPoi(poi); 

    Call <ServerResponse> response = requestInterface.operation(request); 
    response.enqueue(new Callback <ServerResponse>() { 
     @Override 
     public void onResponse(Call <ServerResponse> call, 
           retrofit2.Response <ServerResponse> response) { 
      ServerResponse resp = response.body(); 
      if(resp.getResult().equals(Constants.SUCCESS)) { 
       Toast.makeText(context, resp.getMessage(), Toast.LENGTH_LONG).show(); 
       SharedPreferences.Editor editor = getEditor(); 
       save(editor); 
      } else { 
       Toast.makeText(context, resp.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     } 

     @Override 
     public void onFailure(Call <ServerResponse> call, Throwable t) { 
      Log.d(Constants.TAG, "failed"); 
      Toast.makeText(context, t.getLocalizedMessage(), Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

private void save(SharedPreferences.Editor editor) { 
    editor.putString(Constants.FIRST_POI_ALIAS, poiDialog.getAlias()); 
    editor.putString(Constants.FIRST_POI_KEYWORD, poiDialog.getKeyword()); 
    editor.putString(Constants.FIRST_POI_ADDRESS, poiDialog.getAddress()); 
    editor.putString(Constants.FIRST_POI_CITY, poiDialog.getCity()); 
    editor.apply(); 
} 

private void deletePOI() { 
    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(Constants.BASE_URL) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 
    RequestInterface requestInterface = retrofit.create(RequestInterface.class); 

    User user = new User(); 
    user.setEmail(pref.getString(Constants.USER_EMAIL, "")); 

    POI poi = new POI(); 
    poi.setKeyword(pref.getString(Constants.FIRST_POI_KEYWORD, "")); 
    poi.setCity(pref.getString(Constants.FIRST_POI_CITY, "")); 

    ServerRequest request = new ServerRequest(); 
    request.setOperation(Constants.DELETE_POI); 
    request.setUser(user); 
    request.setPoi(poi); 

    Call <ServerResponse> response = requestInterface.operation(request); 
    response.enqueue(new Callback <ServerResponse>() { 
     @Override 
     public void onResponse(Call <ServerResponse> call, 
           retrofit2.Response <ServerResponse> response) { 

      ServerResponse resp = response.body(); 
      if(resp.getResult().equals(Constants.SUCCESS)) { 
       Toast.makeText(context, resp.getMessage(), Toast.LENGTH_LONG).show(); 
       SharedPreferences.Editor editor = getEditor(); 
       saveDelete(editor); 
       deleted = true; 
      } else { 
       Toast.makeText(context, resp.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     } 

     @Override 
     public void onFailure(Call <ServerResponse> call, Throwable t) { 
      Log.d(Constants.TAG, "failed"); 
      Toast.makeText(context, t.getLocalizedMessage(), Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

private void saveDelete(SharedPreferences.Editor editor) { 
    editor.putString(Constants.FIRST_POI_ALIAS, ""); 
    editor.putString(Constants.FIRST_POI_KEYWORD, ""); 
    editor.putString(Constants.FIRST_POI_ADDRESS, ""); 
    editor.putString(Constants.FIRST_POI_CITY, ""); 
    editor.apply(); 
} 

} 

的index.php:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title></title> 
</head> 
<body> 
    <pre> 
     <?php 

     // dispaly all errors except Warnings 
     ini_set('display_errors',1); 
     error_reporting(E_ALL & ~E_WARNING); 

     include 'fetch_data.php'; 
     require_once 'users/login.php'; 
     require_once 'preferences/preferences.php'; 
     require_once 'include/functions_validation.php'; 

     $login = new Login(); 
     $pref = new Preferences(); 
     $fun = new FunctionsValidation(); 

     if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
      $data = json_decode(file_get_contents("php://input")); 

      if(isset($data -> operation)) { 
       $operation = $data -> operation; 
       if(!empty($operation)) { 
        /* 
        * User registration and login operations 
        */ 
        if($operation == 'register') { 
         echo $login -> register($data); 
        } 
        else if ($operation == 'regVerify') { 
         echo $login -> registerVerify($data); 
        } 
        else if ($operation == 'login') { 
         echo $login -> login($data); 
        } 
        else if ($operation == 'chgPass') { 
         echo $login -> changePassword($data); 
        } 
        else if ($operation == 'resPassReq') { 
         echo $login -> resetPasswordReq($data); 
        } 
        else if ($operation == 'resPass') { 
         echo $login -> resetPassword($data); 
        } 
        /* 
        * Point of Interest operations 
        */ 
        else if($operation == 'addPOI') { 
         echo $pref -> addPOI($data); 
        } 
        else if($operation == 'deletePOI') { 
         echo $pref -> deletePOI($data); 
        } 

        /** 
        * Mileage Distance operations 
        */ 
        else if($operation == 'addMileage') { 
         echo $pref -> addMileage($data); 
        } 
        else if($operation == 'deleteMileage') { 
         echo $pref -> deleteMileage($data); 
        } 
       } else { // if operation is empty 
        echo $fun -> getMsgParamNotEmpty(); 
       } 
      } else { // if operation is not set 
       echo $fun -> getMsgInvalidParam(); 
      } 
     } else if ($_SERVER['REQUEST_METHOD'] == 'GET') { 
      echo "GeolocationNews API"; 
     } 
     ?> 
     </pre> 
    </body> 
</html> 

回答

0

是否Constants.BASE_URL点到你的页面index.php

如果是这样,你试图解析一个HTML页面为JSON,这是无效的。我建议你从你的Android应用程序发送一些查询参数,你可以从PHP中检查,以便输出中不包含HTML部分。

比方说,您的Android应用程序将请求提交给index.php?output=json。然后在你的index.php,你可以检查为:

<?php 
$jsonOnly = (isset($_GET['output']) && $_GET['output'] == 'json'); 

if (!$jsonOnly): 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title></title> 
</head> 
<body> 
    <pre> 
<?php 
endif; 

// your existing PHP code here 

if (!$jsonOnly): 
?> 
    </pre> 
</body> 
</html> 
<?php 
endif; 
?> 

编辑不过,我认为这将是最好的逻辑与演示文稿分开。如果您要将所有PHP代码放在单独的文件中,例如handle_request.php?然后在你的index.php,你可以有你所有的HTML代码,然后包括文件:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title></title> 
</head> 
<body> 
    <pre> 
     <?php include('handle_request.php'); ?> 
    </pre> 
</body> 
</html> 

对于您的应用程序,你可以创建你作为你Constants.BASE_URL使用不同的文件,说app.php,只设置合适的JSON内容类型标题并包含相同的文件:

<?php 
header('Content-Type: application/json'); 
include('handle_request.php'); 
?> 
+0

是的,BASE_URL指向index.php。我已经完成了你在我的index.php中所说的,但我不知道如何发送参数。我编辑了我的问题并发布了Logcat输出的图像,就像你所说的。 –

+0

我对使用Retrofit并不熟悉,但从Google告诉我,通常会有一个界面来定义API端点。在那里,你可以使用@Query()注释来发送一个查询参数(简单地将它添加到你的基础URL将不起作用)。 虽然我认为你可以通过从两个位置访问这些代码来实现自己的生活,但需要JSON和其他HTML。在我看来,将逻辑与演示分离会更好,所以我会更新我的答案。 – rickdenhaan

相关问题