Android App从API返回Null

时间:2017-02-11 15:53:31

标签: c# android json rest

构建Android应用程序以在C#中合并来自RESTful API的Web服务。我希望能够在此阶段检索一些原始JSON以确保连接正常。 API的本地主机URL显示如下数据:

https://github.com/voxpupuli/puppet-prometheus

Android应用程序中的URL字符串设置为外部IP地址,但当我在应用程序上调用数据时,应用程序只显示为空。

Android应用程序代码 - 主要活动:

public class myService extends IntentService {

public static final String TAG = "MyService";
public static final String MY_SERVICE_MESSAGE = "myServiceMessage";
public static final String MY_SERVICE_PAYLOAD = "myServicePayload";

public myService() {
    super("MyService");
}

@Override
protected void onHandleIntent(Intent intent) {
    Uri uri = intent.getData();
    Log.i(TAG, "onHandleIntent: " + uri.toString());

    String response;

    try {
        response =
                HttpHelper.downloadUrl(uri.toString());
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    Intent messageIntent = new Intent(MY_SERVICE_MESSAGE);
    messageIntent.putExtra(MY_SERVICE_PAYLOAD, response);
    LocalBroadcastManager manager =
            LocalBroadcastManager.getInstance(getApplicationContext());
    manager.sendBroadcast(messageIntent);

}

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "onCreate");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "onDestroy");
}
}

我的服务:

public class NetworkHelper {

public static boolean hasNetworkAccess(Context context){

    ConnectivityManager cm = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
}

网络助手:

public class HttpHelper {

/**
 * Returns text from a URL on a web server
 *
 * @param address
 * @return
 * @throws IOException
 */
public static String downloadUrl(String address) throws IOException {

    InputStream is = null;
    try {

        URL url = new URL(address);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect();

        int responseCode = conn.getResponseCode();
        if (responseCode != 200) {
            throw new IOException("Got response code " + responseCode);
        }
        is = conn.getInputStream();
        return readStream(is);

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

/**
 * Reads an InputStream and converts it to a String.
 *
 * @param stream
 * @return
 * @throws IOException
 */
private static String readStream(InputStream stream) throws IOException {

    byte[] buffer = new byte[1024];
    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    BufferedOutputStream out = null;
    try {
        int length = 0;
        out = new BufferedOutputStream(byteArray);
        while ((length = stream.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }
        out.flush();
        return byteArray.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

}

Http Helper Class:

{{1}}

1 个答案:

答案 0 :(得分:0)

首先,确定问题是在服务中还是在您的应用中。

使用所有给定参数从here发出HTTP请求,如果有效,则表示您的服务正常。

现在,请按照以下步骤找出本地问题。

downloadUrl()上运行调试并检查responseCode的值是否为200,然后请求是好的,现在只需清理并重建您的项目。

如果不是200,请按照以下步骤操作:

  1. extends来自AsyncTask
  2. 的HttpHelper课程
  3. 现在将代码从doInBackground()
  4. 移至downloadUrl()
  5. 现在为responseCode调试此类。