我将在我的Android应用程序中添加Spring的RESTful Web Service支持,如https://spring.io/guides/gs/consuming-rest-android/所述.
这是顶级的build.gradle配置:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
这是我的app/build.gradle配置:
apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.example"
        minSdkVersion 8
        targetSdkVersion 17
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    dependencies {
        compile 'com.android.support:appcompat-v7:+'
        compile fileTree(dir: 'libs', include: ['*.jar']) …可能重复需要有关身体参数的RestTemplate Post Request的帮助吗?和Spring REST模板POST但这些答案对我不起作用
 
我试图Instagram通过Spring Android 从API 获取访问令牌.来自Instagram文档的请求如下:
curl \-F 'client_id=CLIENT-ID' \
-F 'client_secret=CLIENT-SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=YOUR-REDIRECT-URI' \
-F 'code=CODE' \https://api.instagram.com/oauth/access_token
这是我的请求访问令牌(在我获得请求令牌成功后):
 MultiValueMap<String, String> mvm = new LinkedMultiValueMap<String, String>();
    mvm.add("client_id", INSTAGRAM_CILENT_ID);
    mvm.add("client_secret", INSTAGRAM_SECRET);
    mvm.add("grant_type", "authorization_code");
    mvm.add("redirect_uri", CALLBACKURL);
    mvm.add("code", requestToken);
    InstagramResult result = restTemplate .postForObject("https://api.instagram.com/oauth/access_token", mvm, InstagramResult .class);
结果映射类:
public class InstagramLogin {
   public String access_token;
   public InstagramUser user;
}
public class InstagramUser {
   public String id;
   public String username; …我正在开发一个从服务器(localhost - mssql和nodejs)接收数据的Android应用程序,保存数据然后显示它
收到服务器响应后,我收到此错误
我按照下面的说明在这里输入链接描述而不是我使用localhost的Web服务器.谢谢
 Illegal character in scheme at index 0: 192.168.2.7:3000
    java.net.URISyntaxException: Illegal character in scheme at index 0: 192.168.2.7:3000
            at java.net.URI.validateScheme(URI.java:419)
            at java.net.URI.parseURI(URI.java:363)
            at java.net.URI.<init>(URI.java:204)
            at cz.uhk.fim.jedlima3.searchrooms.asyncTask.DownloadDatabaseAsync.doInBackground(DownloadDatabaseAsync.java:30)
            at cz.uhk.fim.jedlima3.searchrooms.asyncTask.DownloadDatabaseAsync.doInBackground(DownloadDatabaseAsync.java:15)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            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:864)
使用Maven创建示例Android应用程序
在Android应用程序中使用Spring Android库:
使用Spring Android库中的RestTemplate:
尝试使用Joda将包含日期字符串的JSON字符串反序列化到POJO时,我遇到异常.
我正在使用带有Spring和Robospice的Jackson2.
我收到以下异常:
无法读取JSON:无法从字符串值('2014-07-25T00:00:00')实例化类型[simple type,class org.joda.time.DateTime]的值.没有单字符串构造函数/工厂方法
这是我目前的代码:
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter 
                            = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.getObjectMapper().registerModule(new JodaModule());
msgConverters.add(mappingJackson2HttpMessageConverter);
restTemplate.setMessageConverters(msgConverters);
HttpEntity<?> httpEntity = new HttpEntity<Object>(headers);
final ResponseEntity<HolidayList> responseEntity 
            = restTemplate.exchange(url, HttpMethod.GET, httpEntity,HolidayList.class);
POJO字段的定义如下:
private DateTime departureDate;
我在Jackson1工作过......但似乎无法让它在Jackson2工作.
我通过Spring restTemplate使用android发送图像文件(multipart)有一些问题.
这是我服务器上的控制器:
@RequestMapping(value = "/uploadPhoto/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> uploadPhoto(@RequestParam("file") MultipartFile srcFile,
                                               @PathVariable("id") Integer id) {
///  do something
        return RestUtil.getJsonSHttptatus(HttpStatus.NOT_ACCEPTABLE);
    }
这是我的Android活动中的休息请求
private void doUpload(){
        new AsyncTask<String, Void, String>() {
            @Override
            protected String doInBackground(String... params) {
                FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
                formHttpMessageConverter.setCharset(Charset.forName("UTF8"));
                RestTemplate restTemplate = new RestTemplate();
                restTemplate.getMessageConverters().add( formHttpMessageConverter );
                restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
                restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
                String uri = "http://localhost:8089/web/uploadPhoto/1";
                String imagePath = "/mnt/sdcard/DCIM/Camera/IMG_20140406_130350.jpg";
                MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
                map.add("expenseId", 1);
                map.add("file", new FileSystemResource(path));
                HttpHeaders imageHeaders …我正在使用RoboSpice和Spring Android来执行REST api调用.需要将Content-Type设置为application/x-www-form-urlencoded发送数据.
假设我有一个像这样的Pojo类:
public class Pojo {
  private String pojoAttribute;
  // getter + setter
}
这是我的请求类:
public class PojoRequest extends AbstractApiRequest<PojoResult> {
    private Pojo pojo;
    public PojoRequest(Pojo pojo) {
        super(PojoResult.class);
        this.pojo = pojo;
    }
    @Override
    public PojoResult loadDataFromNetwork() throws Exception {
        HttpEntity<Pojo> requestEntity = new HttpEntity<Pojo>(pojo, getDefaultHeaders());
        String url = buildUrlForPath("mypath");
        RestTemplate restTemplate = getRestTemplate();
        restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
        return restTemplate.postForObject(url, requestEntity, PojoResult.class);
    }
}
所以,假设我需要发送这个主体:
pojo_attribute = foo
现在我的代码不起作用,因为FormHttpMessageConverter不处理POJO.
我希望能做的是这样的:
public class Pojo {
  @FormProperty("pojo_attribute")
  private String pojoAttribute;
  // getter …官方Spring for Android页面提到添加以下依赖项代码.
dependencies {
    compile 'org.springframework.android:spring-android:1.0.1.RELEASE'
}
这是最新版本的build.(根据网站)
我在应用程序的build.gradle中添加了它但是我得到一个错误
Error:Failed to find: org.springframework.android:spring-android:1.0.1.RELEASE
做正确的方法是什么?我可以用同样的方式添加谷歌播放服务作为依赖.
我正在使用android spring RestTemplate在我的应用程序上进行REST服务调用.我添加了android Instrumentation测试,其中包括模拟REST服务调用.从android studio运行时,我的所有测试运行正常,但测试无法在终端使用时加载spring mock类
   ./gradlew clean :app:connectedDebugAndroidTest
spring.gradle上的spring测试依赖项
    androidTestCompile("org.springframework:spring-test:3.2.8.RELEASE")
如果我改为compile而不是androidTestCompile,Android测试从终端正常运行.由于我不希望这种依赖我的生产APK任何帮助,赞赏.
这是开始测试时的日志
08-05 00:04:12.585 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.asm.commons.JSRInlinerAdapter 08-05 00:04:12.587 22274-22302/com.libin .androiduitesting E/TestLoader:找不到类:org.springframework.asm.commons.TryCatchBlockSorter 08-05 00:04:12.593 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework. cglib.transform.AbstractProcessTask 08-05 00:04:12.593 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.cglib.transform.AbstractTransformTask 08-05 00:04:12.603 22274- 22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.core.convert.support.ConvertingPropertyEditorAdapter 08-05 00:04:12.610 22274-22302/com.libin.androiduitesting E/TestLoader:无法find class:org.springframework.core.io.ResourceEditor 08-05 00:04:12.611 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.spring framework.core.io.support.ResourceArrayPropertyEditor 08-05 00:04:12.617 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.http.MockHttpInputMessage 08-05 00:04 :12.618 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.http.MockHttpOutputMessage 08-05 00:04:12.618 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.http.client.MockClientHttpRequest 08-05 00:04:12.618 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.http. client.MockClientHttpResponse 08-05 00:04:12.618 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.jndi.ExpectedLookupTemplate 08-05 00:04:12.619 22274-22302/com.libin.androiduitesting E/TestLoader:找不到类:org.springframework.mock.jndi.SimpleNamingContext 08-05 00:04:12.619 22274-22302/com.libin.androiduitesting …
android spring-test android-espresso spring-android android-instrumentation
spring-android ×10
android ×7
java ×3
spring ×3
gradle ×2
resttemplate ×2
robospice ×2
android-ion ×1
jackson ×1
jodatime ×1
localhost ×1
mapping ×1
maven ×1
rest ×1
retrofit ×1
spring-test ×1