Android 片段 JSON <!DOCTYPE java.lang.String 类型无法转换为 JSON 数组
Android fragment JSON <!DOCTYPE of type java.lang.String cannot be converted to JSONArray
我正在尝试在我的 Android 应用程序中建立与 MYSQL 数据库的连接。
我一直在关注这个教程:http://www.trustingeeks.com/connect-android-app-to-mysql-database/
基本上是根据 JSON 请求填充文本视图。
区别在于我需要让数据连接在 Fragment 中工作。
这是我遇到的问题,没有任何反应。下面是我尝试将教程实现成一个片段:
public class FragmentFour extends Fragment{
private String jsonResult;
private String url = "http://cpriyankara.coolpage.biz/employee_details.php";
private TextView resultView;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_four, container, false);
resultView = (TextView) view.findViewById(R.id.result);
StrictMode.enableDefaults();
getData();
} catch (InflateException e) {
}
return view;
}
public void getData(){
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ieeehiit.host22.com/myfile.php"); //YOUR PHP SCRIPT ADDRESS
// HttpPost httppost = new HttpPost("http://172.23.193.32/elift-test/myfile.php"); //YOUR PHP SCRIPT ADDRESS
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
resultView.setText("Couldnt connect to database");
}///convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}//parse json data
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
s = s +"Name : "+json.getString("id")+" "+json.getString("username"); }
resultView.setText(s);
} catch (Exception e)
{ // TODO:处理异常
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
}
一切正常,但 textView 没有任何反应。我查看了 logcat 并注意到:
"2-09 21:06:05.155 13519-13519/com.example.nottinghamtouristapp E/log_tag﹕ Error Parsing Data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray"
我是 android 开发的新手,所以这个问题很可能是由于我缺乏 fragments.I 方面的经验,如果我显得愚蠢和傲慢地发帖寻求帮助,请道歉。感觉脑子里想的都用完了!
非常感谢您。
好的试试这个方法
初始化变量
private View rootView;
private ListView lv;
private String jsonResult;
private String url = "http://cpriyankara.coolpage.biz/employee_details.php";
ProgressDialog pDialog;
private TextView resultView;
修改你的 AsyncTask
public class JsonReadTask extends AsyncTask<String , Void, String> {
public JsonReadTask() {
super();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity(), ProgressDialog.THEME_DEVICE_DEFAULT_DARK);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setIndeterminate(true);
pDialog.setMessage("Your message");
pDialog.setCancelable(false);
pDialog.setInverseBackgroundForced(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
customList = new ArrayList<>();
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("array-name");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("name");
}
return name;
} catch (Exception e) {
e.printStackTrace();
getActivity().finish();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
} catch (Exception e) {
getActivity().finish();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
if(customList == null){
Log.d("ERORR", "No result to show.");
return;
}
resultView.setText(result);
pDialog.dismiss();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
task.execute(new String[]{url});
}
希望对您有所帮助!!!
我正在尝试在我的 Android 应用程序中建立与 MYSQL 数据库的连接。
我一直在关注这个教程:http://www.trustingeeks.com/connect-android-app-to-mysql-database/
基本上是根据 JSON 请求填充文本视图。
区别在于我需要让数据连接在 Fragment 中工作。
这是我遇到的问题,没有任何反应。下面是我尝试将教程实现成一个片段:
public class FragmentFour extends Fragment{
private String jsonResult;
private String url = "http://cpriyankara.coolpage.biz/employee_details.php";
private TextView resultView;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_four, container, false);
resultView = (TextView) view.findViewById(R.id.result);
StrictMode.enableDefaults();
getData();
} catch (InflateException e) {
}
return view;
}
public void getData(){
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ieeehiit.host22.com/myfile.php"); //YOUR PHP SCRIPT ADDRESS
// HttpPost httppost = new HttpPost("http://172.23.193.32/elift-test/myfile.php"); //YOUR PHP SCRIPT ADDRESS
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
resultView.setText("Couldnt connect to database");
}///convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}//parse json data
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
s = s +"Name : "+json.getString("id")+" "+json.getString("username"); }
resultView.setText(s);
} catch (Exception e)
{ // TODO:处理异常
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
}
一切正常,但 textView 没有任何反应。我查看了 logcat 并注意到:
"2-09 21:06:05.155 13519-13519/com.example.nottinghamtouristapp E/log_tag﹕ Error Parsing Data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray"
我是 android 开发的新手,所以这个问题很可能是由于我缺乏 fragments.I 方面的经验,如果我显得愚蠢和傲慢地发帖寻求帮助,请道歉。感觉脑子里想的都用完了!
非常感谢您。
好的试试这个方法
初始化变量
private View rootView;
private ListView lv;
private String jsonResult;
private String url = "http://cpriyankara.coolpage.biz/employee_details.php";
ProgressDialog pDialog;
private TextView resultView;
修改你的 AsyncTask
public class JsonReadTask extends AsyncTask<String , Void, String> {
public JsonReadTask() {
super();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity(), ProgressDialog.THEME_DEVICE_DEFAULT_DARK);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setIndeterminate(true);
pDialog.setMessage("Your message");
pDialog.setCancelable(false);
pDialog.setInverseBackgroundForced(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
customList = new ArrayList<>();
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("array-name");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("name");
}
return name;
} catch (Exception e) {
e.printStackTrace();
getActivity().finish();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
} catch (Exception e) {
getActivity().finish();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
if(customList == null){
Log.d("ERORR", "No result to show.");
return;
}
resultView.setText(result);
pDialog.dismiss();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
task.execute(new String[]{url});
}
希望对您有所帮助!!!