接收数据并显示在日志中。但是我似乎无法在屏幕上显示它。它一直说java.lang.String无法转换为JSONObject。但是我在另一个项目中尝试了这些行,似乎工作正常。
这是非常感谢的代码:
MainActivity.java
package synctc.me.rssfeed;
import android.nfc.Tag;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import butterknife.ButterKnife;
import butterknife.InjectView;
public class MainActivity extends ActionBarActivity {
public static final String TAG = MainActivity.class.getSimpleName();
private Current mCurrent ;
@InjectView(R.id.title)TextView mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
getUrl();
Log.d(TAG,"Main UI is Running");
}
private void getUrl(){
String url ="https://www.facebook.com/feeds/page.php?format=rss20&id=311612925545909";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Call call=client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
@Override
public void onResponse(Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
try {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
if(response.isSuccessful()){
mCurrent = getCurrentDetails(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateDisplay();
}
});
}else{
}
} catch (IOException e) {
Log.e(TAG, "Exception Caught : ",e);
}
catch (JSONException e){
Log.e(TAG, "Exception Caught : ",e);
}
}
});}
private void updateDisplay (){
mTitle.setText(mCurrent.getTitle()+"");
}
private Current getCurrentDetails(String jsonData) throws JSONException{
JSONObject channel = new JSONObject (jsonData);
String channelVal = channel.getString("channel");
JSONObject currently = new JSONObject(channel.get("channel").toString());
Current current = new Current();
current.setTitle(currently.getString("title"));
Log.i(TAG,"From JSON To:"+channelVal);
return current;
}
}
Current.java
public class Current {
private String mChannel;
private String mTitle;
private String mDescription;
private String mImg;
public String getDescription() {
return mDescription;
}
public void setDescription(String description) {
mDescription = description;
}
public String getImg() {
return mImg;
}
public void setImg(String img) {
mImg = img;
}
public String getTitle() {
return mTitle;
}
public void setTitle(String title) {
mTitle = title;
}
public String getChannel() {
return mChannel;
}
public void setChannel(String channel) {
mChannel = channel;
}
}
这是logcat
<rss version="2.0"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
>
<channel>
<title>حائط Professional Hackers على فيسبوك</title>
<link>https://www.facebook.com/</link>
<description>حائط Professional Hackers على فيسبوك</description>
<language>en-us</language>
<category domain="Facebook">PageSyndicationFeed</category>
<generator>Facebook Syndication</generator><docs>http://www.rssboard.org/rss-specification</docs>
<lastBuildDate>Mon, 08 Sep 2014 10:48:32 +0000</lastBuildDate>
<webMaster>webmaster@facebook.com</webMaster>
<item>
<guid isPermaLink="false">www.facebook.com/notification/847a02dda3e67da1443312712cc478b4</guid>
<title><![CDATA[ ]]></title>
<link>http://www.facebook.com/ProfessionalHackers00/photos/a.342883082418893.81113.311612925545909/825529307487599/?type=1</link>
<description><![CDATA[<a href="/ProfessionalHackers00/photos/a.342883082418893.81113.311612925545909/825529307487599/?type=1&relevant_count=1" id="" title="" target="" onclick="" style=""><img class="img" src="https://m.ak.fbcdn.net/sphotos-g.ak/hphotos-ak-xta1/v/t1.0-9/s130x130/10665141_825529307487599_4548128653985487900_n.png?oh=6469cb21ce0091796b296364f61232fa&oe=55AB5930&__gda__=1436725829_d9cda3c297f41ce6485f644c528b429e" alt="" /></a><br/>]]></description>
<pubDate>Mon, 08 Sep 2014 11:48:32 +0100</pubDate>
<author>Professional Hackers</author>
<dc:creator>Professional Hackers</dc:creator>
</item>
<item>
<guid isPermaLink="false">www.facebook.com/notification/d22710b9bde130cf6d5efda7d4256f7e</guid>
<title><![CDATA[ Tired of Blocked Websites? !! This trick will surely help you
Top methods to...]]></title>
<link>http://www.facebook.com/ProfessionalHackers00/posts/825529050820958</link>
<description><![CDATA[Tired of Blocked Websites? !! This trick will surely help you <br /> <br /> Top methods to open / access blocked sites without PROXY <br /> <br /> Visit: <a href="http://l.facebook.com/l.php?u=http%3A%2F%2Fgoo.gl%2FVL0Hb9&h=GAQGfLVzP&s=1" rel="nofollow nofollow" target="_blank" onmouseover="LinkshimAsyncLink.swap(this, "http:\/\/goo.gl\/VL0Hb9");" onclick="LinkshimAsyncLink.swap(this, "http:\/\/l.facebook.com\/l.php?u=http\u00253A\u00252F\u00252Fgoo.gl\u00252FVL0Hb9&h=GAQGfLVzP&s=1");">http://goo.gl/VL0Hb9</a><br /> <br/><br/><a href="http://l.facebook.com/l.php?u=http%3A%2F%2Fgoo.gl%2FVL0Hb9&h=cAQG3TXIB&s=1" id="" title="" target="" onclick="LinkshimAsyncLink.swap(this, "http:\/\/l.facebook.com\/l.php?u=http\u00253A\u00252F\u00252Fgoo.gl\u00252FVL0Hb9&h=cAQG3TXIB&s=1");" style="" rel="nofollow" onmouseover="LinkshimAsyncLink.swap(this, "http:\/\/goo.gl\/VL0Hb9");"><img class="img" src="https://m.ak.fbcdn.net/external.ak/safe_image.php?d=AQCqiahUKnLbcfwh&w=158&h=158&url=https%3A%2F%2Fscontent-a-vie.xx.fbcdn.net%2Fhphotos-xpf1%2Fv%2Ft1.0-9%2F10603264_825528994154297_1736307390360087029_n.png%3Foh%3Dc48a5971b5dfc19555267662e13c219a%26oe%3D54949E9D" alt="" /></a><br/><a href="http://l.facebook.com/l.php?u=http%3A%2F%2Fgoo.gl%2FVL0Hb9&h=WAQFr5mbg&s=1" id="" target="_blank" onclick="LinkshimAsyncLink.swap(this, "http:\/\/l.facebook.com\/l.php?u=http\u00253A\u00252F\u00252Fgoo.gl\u00252FVL0Hb9&h=WAQFr5mbg&s=1");" style="" rel="nofollow" onmouseover="LinkshimAsyncLink.swap(this, "http:\/\/goo.gl\/VL0Hb9");">Top methods to open / access blocked sites without PROXY | Cool Tips and Tricks</a><br/>www.gotipsandtrick.info]]></description>
<pubDate>Mon, 08 Sep 2014 11:47:17 +0100</pubDate>
<author>Professional Hackers</author>
<dc:creator>Professional Hackers</dc:creator>
</item>
<item>
<guid isPermaLink="false">www.facebook.com/notification/d5aa50993c46015a9af
04-08 07:29:00.338 1875-1893/synctc.me.rssfeed E/MainActivity﹕ Exception Caught :
org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:160)
at org.json.JSONObject.<init>(JSONObject.java:173)
at synctc.me.rssfeed.MainActivity.getCurrentDetails(MainActivity.java:105)
at synctc.me.rssfeed.MainActivity.access$100(MainActivity.java:27)
at synctc.me.rssfeed.MainActivity$1.onResponse(MainActivity.java:73)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:162)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:0)
通过访问facebook链接获得的响应是XML格式。
当解析器尝试读取您的文档时,它会尝试转换
?xml的
进入json