Web服务不起作用(Android Studio)

时间:2016-06-30 21:31:45

标签: java android json web-services android-studio

所以我第一次尝试编写Web服务,这对我来说真的很难...我想要的是获取一个包含整个JSON代码的字符串,但是由于某种原因我没有得到它。

AndroidManifest(忽略应用名称):

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rome.networkplis">
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java:

package com.example.rome.networkplis;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    TextView textView;
    String h = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        WebService webService = new WebService();
        h = webService.json();
        if (h == null)
            textView.setText("not working");
        else
            textView.setText(h);
    }
}

WebService类:

package com.example.rome.networkplis;


import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class WebService extends AsyncTask<String, Void, String> {
    String h = null;
    @Override
    protected String doInBackground(String... params) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                URLConnection urlConnection = null;
                InputStream in = null;
                try {
                    URL url = new URL("http://eanousa.tumblr.com/api/read/json");
                    urlConnection = url.openConnection();
                    in = urlConnection.getInputStream();
                    h = getStringFromInputStream(in);
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        Thread thread = new Thread(r);
        thread.start();
        return h;
    }


    public String json() {
        String title = null;
        try {
            JSONObject everything = new JSONObject(doInBackground(""));
            JSONObject thumbObj = everything.getJSONObject("tumblelog");
            title = thumbObj.getString("title");
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return title;

    }
    private static String getStringFromInputStream(InputStream is) {

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();

        String line;
        try {

            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return sb.toString();

    }
}

感谢您抽出宝贵时间并尝试帮助我。

2 个答案:

答案 0 :(得分:1)

您的WebService课程延伸AsyncTask,因此您只应在execute()上致电WebService以开始执行任务。在execute()上拨打AsyncTask会自动从后台线程调用您的runInBackground()方法。在那里,您不需要创建新的Runnable。您可以只调用您的Web服务,然后将响应作为字符串返回。无论您的runInBackground方法返回什么,都会将onPostExecute作为参数发送给onPostExecute。在msi中,您将向响应者通知响应。

答案 1 :(得分:0)

您不需要Runnable对象,只需直接在doInBackground方法中运行代码。