2013-10-13 1 views
0

내 코드를 다음과 같이 만들려고했습니다. https://github.com/jbeerdev/BrightHubCode/blob/master/src/com/bright/hub/background/WebServiceBackgroundActivity.java 이것은 내가 비동기 클래스로 호출하고 보내는 방법입니다. MainActivity에 모든 코드를 입력하면 작동하지만 외부 클래스를 통해이 코드를 가져올 수 없습니다. "java.lang.String의가 [] 자바로 캐스팅 할 수 없습니다 https://github.com/jbeerdev/BrightHubCode/blob/master/src/com/bright/hub/background/WebServiceAsyncTask.java 가^나는 이러한 종류의 오류를 얻을 :jsonAsync 코드를 별도의 클래스에 넣는 중 ...

내 MainActivity 코드 :

public class MainActivity extends FragmentActivity implements ActionBar.OnNavigationListener { 

private static final String url = "http://www.somesite.com/JSON.php"; 
public String post; 
public ListView listView; 
public String jsonResult; 

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
initializeDialog(); 
setContentView(R.layout.activity_main); 
listView = (ListView) findViewById(R.id.listView1); 
accessWebService(); 
} 
private void accessWebService() { 
    JsonReadTask jsonTask = new JsonReadTask(); 
    jsonTask.execute(new String[] { url, post },this); 
} 

private void initializeDialog() { 
    dialog = ProgressDialog.show(MainActivity.this, "", "LoadingData. Wait...", true); 
    dialog.show(); 
    } 

    public void ListDrawer() { 
    List<Map<String, String>> galleryList = new ArrayList<Map<String, String>>(); 

    try { 

    JSONObject jsonResponse = new JSONObject(jsonResult); 
    JSONArray jsonMainNode = jsonResponse.optJSONArray("galleries"); 

    for (int i = 0; i < jsonMainNode.length(); i++) { 
    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 

    String loc = jsonChildNode.optString("loc"); 
    String foto = jsonChildNode.optString("foto"); 
    String outPut = "http://www.somesite.com/images/" + loc + foto; 
    galleryList.add(createGallery("galleries", outPut)); 
    } 
    } catch (JSONException e) { 
    //Toast.makeText(getApplicationContext(), "Error" + e.toString(), 
    //Toast.LENGTH_LONG).show(); 
    } 

} 

그러나 비동기 클래스 (문제 부분) .lang.String " 이렇게되는 곳 :"String serviceUrl = (String) params [0]; " 내가 문제가있는 곳이야 ..? (중요 부분) 부팅

public class JsonReadTask extends AsyncTask<Object, Void, String> { 
    MainActivity callerActivity; 
    @Override 
    public String doInBackground(Object... params) { 

    callerActivity = (MainActivity) params[1]; 
    HttpClient httpclient = new DefaultHttpClient(); 
    String theurl = (String) params[0]; //urlJSON 

    HttpPost httppost = new HttpPost(theurl); 
    try { 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    String thepost = (String) params[1]; //tablename 
    nameValuePairs.add(new BasicNameValuePair("getgallery", "thepost")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    HttpResponse response = httpclient.execute(httppost); 


     callerActivity.jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); 
    } 

    catch (ClientProtocolException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
    return null; 
    } 

애플 리케이션 충돌이주는 대화 그것을 :

내 비동기 클래스 코드 "doInBackground()를 실행하는 동안 오류가 발생" 그것은 적어도 "doInBackground"에의 추락했다. 도움에 미리 감사드립니다!

편집 내 로그 캣 : http://pastebin.com/XZaNZTTg

+0

충돌 로그 게시! – Nerd

+0

좋아, 내 로그 고양이입니다 : http://pastebin.com/XZaNZTTg – Arndroid

답변

1

당신은 충돌 로그에 두 가지 중요한 라인이 있습니다

10-13 11:49:43.881: E/AndroidRuntime(7289): Caused by: java.lang.ClassCastException: java.lang.String[] cannot be cast to java.lang.String 
10-13 11:49:43.881: E/AndroidRuntime(7289):  at com.example.providenceartapp.JsonReadTask.doInBackground(JsonReadTask.java:31) 

어딘가에 당신이 하나의 문자열로 문자열 배열을 사용하려는를, 그것뿐만 보일 것 여기에있는 경우

String theurl = (String) params[0]; //urlJSON; 

params[0]은 배열입니다.

+0

jsonTask.execute (new String [] {url, post}, this); <이것은 Async 클래스에 문자열을 보내는 방법입니다. params []는 문자열 배열에서 문자열을 가져 오는 것입니다. 그것을 변환 할 수있는 어떤 방법이든 작동할까요? 아니면 우회? – Arndroid

+0

문자열 배열과 활동을 전달하므로'params [0]'은 문자열 배열입니다. URL을 얻고 싶다면'String theurl = params [0] [0]' –

+0

을 입력하면됩니다. "표현식의 유형은 배열 유형이어야하지만 객체로 해석되어야합니다" – Arndroid

관련 문제