retrofit.RetrofitError请求网络执行期间发生异常:null

时间:2015-06-27 14:36:07

标签: java android retrofit robospice

当我尝试运行下面给出的RoboSpice-Retrofit示例时

https://github.com/octo-online/RoboSpice-samples/tree/release/robospice-sample-retrofit

我有一个错误retrofit.RetrofitError请求网络执行期间发生异常:null

我已经完成了这个问题 Uploading an image to Google appengine using Robospice with Retrofit

但这并没有解决我的问题。

这是我的RetrofitActivity.java

package com.example.dinesh.basicfragments;

import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.SpiceException;


public class RetrofitActivity extends BaseSampleSpiceActivity{


private TextView mTextView;

private SampleRetrofitSpiceRequest githubRequest;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.spice_activity);

    mTextView = (TextView) findViewById(R.id.spice_tv);

    githubRequest = new SampleRetrofitSpiceRequest("octo-online","robospice");



}

@Override
protected void onStart() {
    super.onStart();
    getSpiceManager().execute(githubRequest,"github", DurationInMillis.ONE_MINUTE,new ListContributorRequestListener());
}

private class ListContributorRequestListener implements com.octo.android.robospice.request.listener.RequestListener<Contributor.List> {
    @Override
    public void onRequestFailure(SpiceException e) {
        e.printStackTrace();
        Toast.makeText(RetrofitActivity.this,"Failure Buddy",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onRequestSuccess(Contributor.List contributors) {
        Toast.makeText(RetrofitActivity.this,"Success Buddy",Toast.LENGTH_LONG).show();
        updateContributors(contributors);
    }
}

private void updateContributors(Contributor.List contributors) {

    String originalText = "RoboSpice Contributors : ";

    StringBuilder builder = new StringBuilder();
    builder.append(originalText);
    builder.append('\n');
    builder.append('\n');
    for (Contributor contributor : contributors) {
        builder.append('\t');
        builder.append(contributor.login);
        builder.append('\t');
        builder.append('(');
        builder.append(contributor.contributions);
        builder.append(')');
        builder.append('\n');
    }
    mTextView.setText(builder.toString());
  }
  }

SampleRetrofitSpiceRequest.java

package com.example.dinesh.basicfragments;

import com.octo.android.robospice.request.retrofit.RetrofitSpiceRequest;
import com.octo.android.robospice.retrofit.RetrofitGsonSpiceService;

import roboguice.util.temp.Ln;


public class SampleRetrofitSpiceRequest extends    RetrofitSpiceRequest<Contributor.List,GitHub> {

private String owner ;
private String repo;
public SampleRetrofitSpiceRequest(String owner , String repo) {
    super(Contributor.List.class,GitHub.class);
    this.owner=owner;
    this.repo=repo;
}

@Override
public Contributor.List loadDataFromNetwork() throws Exception {
    Ln.d("Call Web Service");
    return getService().contributors(owner,repo);
  }
 }

BaseSampleSpiceActivity.java

package com.example.dinesh.basicfragments;

import android.app.Activity;
import com.octo.android.robospice.SpiceManager;
public abstract class BaseSampleSpiceActivity extends Activity {

private SpiceManager spiceManager = new SpiceManager(SampleRetrofitService.class);

@Override
protected void onStart() {
    spiceManager.start(this);
    super.onStart();
}

@Override
protected void onStop() {
    spiceManager.shouldStop();
    super.onStop();
}

protected SpiceManager getSpiceManager() {
    return spiceManager;
  }
 }

接口GitHub.java

package com.example.dinesh.basicfragments;

import java.net.URLEncoder;

import retrofit.http.EncodedPath;
import retrofit.http.GET;
import retrofit.http.Path;


public interface GitHub {



@GET("/repos/{owner}/{repo}/contributors")
Contributor.List contributors(@Path("owner") String owner, @Path("repo") String repo);
 }

SampleRetrofitService.java

package com.example.dinesh.basicfragments;

import android.app.Application;

import com.octo.android.robospice.SpiceService;
import com.octo.android.robospice.persistence.CacheManager;
import com.octo.android.robospice.persistence.exception.CacheCreationException;
import com.octo.android.robospice.retrofit.RetrofitGsonSpiceService;

public class SampleRetrofitService extends RetrofitGsonSpiceService {

private static final String BASE_URL = "https://api.github.com";

@Override
public void onCreate() {
    super.onCreate();
    addRetrofitInterface(GitHub.class);
}

@Override
protected String getServerUrl() {
    return BASE_URL;
  }
 }

Contributor.java

package com.example.dinesh.basicfragments;


import java.util.ArrayList;

public class Contributor {
public String login;
public int contributions;

@SuppressWarnings("serial")
public static class List extends ArrayList<Contributor> {
  }
 }

这是我的StackTrace

06-27 20:30:56.074  25522-25732/com.example.dinesh.basicfragments E//RequestRunner.java:134﹕ 20:30:56.088 Thread-37297 An exception occurred during request network execution :null
retrofit.RetrofitError
        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:343)
        at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:222)
        at $Proxy0.contributors(Native Method)
        at com.example.dinesh.basicfragments.SampleRetrofitSpiceRequest.loadDataFromNetwork(SampleRetrofitSpiceRequest.java:24)
        at com.example.dinesh.basicfragments.SampleRetrofitSpiceRequest.loadDataFromNetwork(SampleRetrofitSpiceRequest.java:11)
        at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:45)
        at com.octo.android.robospice.request.RequestRunner.processRequest(RequestRunner.java:130)
        at com.octo.android.robospice.request.RequestRunner$1.run(RequestRunner.java:197)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
06-27 20:31:02.604  25522-25732/com.example.dinesh.basicfragments E//RequestRunner.java:134﹕ 20:31:02.613 Thread-37297 An exception occurred during request network execution :null
retrofit.RetrofitError
        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:343)
        at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:222)
        at $Proxy0.contributors(Native Method)
        at com.example.dinesh.basicfragments.SampleRetrofitSpiceRequest.loadDataFromNetwork(SampleRetrofitSpiceRequest.java:24)
        at com.example.dinesh.basicfragments.SampleRetrofitSpiceRequest.loadDataFromNetwork(SampleRetrofitSpiceRequest.java:11)
        at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:45)
        at com.octo.android.robospice.request.RequestRunner.processRequest(RequestRunner.java:130)
        at com.octo.android.robospice.request.RequestRunner$1.run(RequestRunner.java:197)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

实际上问题是该名称没有存在repo.So,我通过“stephanenicolas”而不是“octo-online”作为参数。这对我有用