2014-09-03 43 views
0

我有一个关于Public Transportation的Android应用程序,我有一个连接到mySQL数据库的PHP脚本。这是main.java错误org.json.JSONException:值<html><head><title> java.lang.String类型的错误无法转换为JSONObject

package com.chera.trans; 

import com.chera.trans.R; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 

    public class Main extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu_main, menu); 
     return true; 
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
    case R.id.traseu2: 
     Intent traseu2 = new Intent(this, Traseu2.class); 
     this.startActivity(traseu2); 
     break; 
    case R.id.traseu401: 
     Intent traseu401 = new Intent(this, Traseu401.class); 
     this.startActivity(traseu401); 
     break; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 

    return true; 
} 

} 这是Traseu2.java

package com.chera.trans; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 




import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
    import org.apache.http.client.HttpClient; 
    import org.apache.http.client.methods.HttpPost; 
    import org.apache.http.impl.client.DefaultHttpClient; 
    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 




    import com.chera.trans.R; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
    import android.content.Intent; 
    import android.view.Menu; 
    import android.view.MenuInflater; 
    import android.view.MenuItem; 
    import android.widget.ListView; 
import android.widget.SimpleAdapter; 
    import android.widget.Toast; 

public class Traseu2 extends Activity { 
private String jsonResult; 
private String url = "http://transploiesti.tk/2.php"; 
private ListView listView; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.traseu2); 
    listView = (ListView) findViewById(R.id.listView1); 
    accessWebService(); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu_main, menu); 
     return true; 
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
    case R.id.traseu2: 
     Intent traseu2 = new Intent(this, Traseu2.class); 
     this.startActivity(traseu2); 
     break; 
    case R.id.traseu401: 
     Intent traseu401 = new Intent(this, Traseu401.class); 
     this.startActivity(traseu401); 
     break; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 

    return true; 
} 

// Async Task to access the web 
private class JsonReadTask extends AsyncTask<String, Void, String> { 
    @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(); 
    } 

    catch (ClientProtocolException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
    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 (IOException e) { 
    // e.printStackTrace(); 
    Toast.makeText(getApplicationContext(), 
     "Error..." + e.toString(), Toast.LENGTH_LONG).show(); 
    } 
    return answer; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
    ListDrwaer(); 
    } 
}// end async task 

public void accessWebService() { 
    JsonReadTask task = new JsonReadTask(); 
    // passes values for the urls string array 
    task.execute(new String[] { url }); 
} 

// build hash set for list view 
public void ListDrwaer() { 
    List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>(); 

    try { 
    JSONObject jsonResponse = new JSONObject(jsonResult); 
    JSONArray jsonMainNode = jsonResponse.optJSONArray("traseudoi"); 

    for (int i = 0; i < jsonMainNode.length(); i++) { 
    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
    String name = jsonChildNode.optString("Statie"); 
    String number = jsonChildNode.optString("Oraplecare"); 
    String outPut = "Autobuzul pleaca din " + name + " la ora " + number; 
    employeeList.add(createEmployee("employees", outPut)); 
    } 
    } catch (JSONException e) { 
    Toast.makeText(getApplicationContext(), "Error" + e.toString(), 
    Toast.LENGTH_SHORT).show(); 
    } 

    SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList, 
    android.R.layout.simple_list_item_1, 
    new String[] { "employees" }, new int[] { android.R.id.text1 }); 
    listView.setAdapter(simpleAdapter); 
} 

private HashMap<String, String> createEmployee(String name, String number) { 
    HashMap<String, String> employeeNameNo = new HashMap<String, String>(); 
    employeeNameNo.put(name, number); 
    return employeeNameNo; 
} 

} 代码`为什么我的时候我米运行的应用程序收到此错误?谢谢!

+0

粘贴你的php编码 – Jamil 2014-09-03 21:02:01

回答

相关问题