问题
I started a Parse server instance on AWS where I want to store my data from an android application.
In the module's gradle I inserted the below under dependencies
implementation 'com.parse.bolts:bolts-tasks:1.4.0'
implementation 'com.parse:parse-android:1.17.3'
and created a new application which initializes parse configuration
import android.app.Application;
import android.app.Application;
import android.util.Log;
import com.parse.Parse;
import com.parse.ParseACL;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.SaveCallback;
public class StarterApp extends Application {
@Override
public void onCreate() {
super.onCreate();
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
// Add your initialization code here
Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
.applicationId(myAppId)
.clientKey(myClientKey)
.server(serverAddress)
.build()
);
ParseObject object = new ParseObject("ExampleObject");
// object.put("id", "123");
// object.put("name", "jack");
object.saveInBackground(new SaveCallback () {
@Override
public void done(ParseException ex) {
if (ex == null) {
Log.i("Parse Result", "Successful!");
} else {
Log.i("Parse Result", "Failed" + ex.toString());
}
}
});
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
defaultACL.setPublicWriteAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
}
This is all new to me and I am following a 3-year-old course on Android development. when I run this, the exception keeps getting caught with the same error:
com.parse.ParseRequest$ParseRequestException: i/o failure
My android Manifest looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.parsetest2">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:name=".StarterApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
Where is the error coming from? how can it be solved?
回答1:
The Main problem is that you are not connecting to the server.
[1]-make sure you have added your application id and client id in AndroidManifest
**change _App_Id_here and _Client_key_here with your keys something like "ISIGDGU4YGCE673".
<meta-data
android:name="com.parse.SERVER_URL"
android:value="https://parseapi.back4app.com" />
<meta-data
android:name="com.parse.APPLICATION_ID"
android:value="_App_Id_Here_" /> //<- put your key
<meta-data
android:name="com.parse.CLIENT_KEY"
android:value="_client_Key_here_" /> //<- put your key
[2]- add repositories { maven { url 'https://jitpack.io' } }
to your build.gradle(Project:xyz)
[3]- make sure u change the.server field to your parse server, AppId and clientKey:
Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
.applicationId(myAppId)//"myAppId" should be sth like ->"ASHWD63787EDXSG8"
.clientKey(myClientKey)//should be sth like ->"tdf29yedih287823"
.server(serverAddress)//something like-> "https://parseapi.back4app.com"
.build()
);
get those keys from your server's dashboard settings -> keys
回答2:
This problem can also occur in case your hosted parse server is on HTTP server rather than on HTTPS. Because Android 9.0 and above will not let you use clear text information over the HTTP. So if it is the case then you can do the following:
- In you manifest file, and in your manifest tag add the
android:targetSandboxVersion="1"
line. So it will look like
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"
android:targetSandboxVersion="1">
- In your manifest file, in application tag add the
android:usesCleartextTraffic="true"
line. So it will look like
<application
android:name=".Example"
android:allowBackup="true"
....
android:usesCleartextTraffic="true">
回答3:
Fixed. For some reason the Parse server dashboard which comes from the AWS service. That is for example,http://ec2-18-220-53-0.us-east-2.compute.amazonaws.com doesnt have an apps category, You'd notice it only shows my Bitnami Parse API without an apps option. This means firstly, you're not able to pass data to any app on the server which should be available. Secondly, connecting the server to your android app would not work as the Master key is actually now different from the Client key in recent times.
Instead of AWS use Back4App which would work the same exact way. Back4App is a scalable back-end service based on the Parse Server Platform.
Pros:
You dont need to use an SSH client to connect to server and retrieve your APP ID, Client Key or server url. Everything is under server setting on the dashboard.
Proceed to Back4App:
Step 1: Create an Account on back4app
Step 2: Create an App, you can name it instagramandroid based on this tutorial
Step 3: follow all the remaining info in the link here and every thing should work fine . https://www.back4app.com/docs/pages/android/how-to-build-an-android-app-on-back4app
Note: When adding the parse dependency, the latest one is 1.17.3. which would work very fine unless you may want to use 1.20.0, i tried it and doesn't really work. Add it like you'll normally add a dependency in your gradle file : 'implementation com.parse:parse-android:1.17.3'
来源:https://stackoverflow.com/questions/54743209/java-android-i-o-failure-exception-when-creating-a-parseobject